After some trial and a lot of error, I have come to find out that it is not very useful to template operators. As an Example:
class TemplateClass
{
//Generalized template
template<TType>
TType& operator[](const std::string& key)
{
return TType;
}
//Specialized template for int
template<>
int& operator[]<int>(const std::string& key)
{
return 5;
}
//Specialized template for char
template<>
char& operator[]<char>(const std::string& key)
{
return 'a';
}
}
int main()
{
TemplateClass test;
//this will not match the generalized template.
test["test without template will fail"];
//this is not how you call an operator with template.
test<char>["test with template will fail"];
//this works!
test.operator[]<int>["test will work like this"];
return 0;
}
So to make an operator with a template is pretty ugly (unless you are into verbose, and really who is?) This is why I have been using a function "get" in lieu of operators. My question is why the ugliness? Why is it required to include the operator key word. My guess is that it has to do with some back end magic of transforming operators to not use parentheses to take arguments, and is there any way around this? Thanks in Advance.