templated subscript operator?

Started by
10 comments, last by Perost 19 years, 3 months ago
Following up on what Deyja said: the template could (if I'm not smoking anything) also be disambiguated by assigning to a temporary, like this:

int w = config["width"];int h = config["height"];SetVideo(w, h);


Or you could (again, unless I'm smoking something) make the operator[] take a second dummy parameter of the required type, like this:

template<class T> const T& Config::operator[] (const string &name, T dummy) {  std::string result = mReader[name];  if(result.empty()) return StringTo<T>(mDefaults[name]);  return StringTo<T>(result);}// The "int(0)"s here should provide anonymous, auto storage ints.// But I'm not sure you can really do that...SetVideo(config["width", int(0)], config["height", int(0)]);


Or you could cause the Config class to maintain the association between key names and types... although I can't think of a way to do that without RTTI :s
Advertisement
Zahlman, I don't think that you first suggestion works at all. If I try something like that, I only get that there is no such function. That is, you need to use syntax that Deyja showed. However, I think that your second suggestion would work. I've actually been thinking about it myself, but that would actually make it longer. But thanks for the suggestions.

This topic is closed to new replies.

Advertisement