I'm writing a config class for my game, and you can use the subscript operator to retrive values, like:
int width = config["width"];
But since it returns strings this doesn't work. I thought that I'd overload the subscript operator so that it returns any type with the help of templates. I wrote this function:
template<class T> const T& Config::operator[] (const string &name)
{
if(mReader[name].empty())
{
return StringTo<T>(mDefaults[name]);
}
return StringTo<T>(mReader[name]);
}
This function checks whether a value has been read from file, otherwise it returns a default value. StringTo<T> converts the string to type T.
But this doens't work. I get "../game/Game.cpp:55: error: no match for 'operator[]' in 'Singleton<T>::Instance [with T = Config]()["width"]'" from the compiler when I'm compiling it. In normal templated functions you put <type> after the function name when calling it, but where to put it in this case? I've searched like a madman after a solution, but I can't find anything [crying]. If anyone know the answer to this, then I'd really appreciate some help.