Not worth it.
Templating the return type means you'd have to explicitly specify the template parameter when you call it. Something like this, maybe I have the syntax wrong:
int i = obj.operator[]<int>("IntVal");
C++ does not deduce template parameters from what you assign the result of the call to, only from the parameters you call the function with.
So you might as well just define a normal function:
int i = obj.get<int>("IntVal");
Or in this case, either do this or implement get
using this:
int i = boost:lexical_cast<int>(obj["IntVal"]);
As Amit says, you could define operator[]
to return a type which can be converted either to int
or to other types. Then your example code can be made to compile without the explicit lexical_cast.