I have a class Myclass with a template override on the + operator. The function should be able to take several different types as an argument. I would prefer to just have a single template function declaration, but that means all the types passed-in have to be able to conform to const T & any_type
. I need the arguments to be passed-in by reference. No big deal except when I come to a c-string or char*
. I can't figure out how to specialize the function for c-strings without having a second function template declaration in the header file.
// myclass.h
template <typename T>
void operator + (const T & any_type);
// myclass.cpp
// if passing in a std::string
template <>
void Myclass::operator + <std::string> (const std::string & my_c_string){ // works no problem
// do stuff
}
// if passing in a custom class
template <>
void Myclass::operator + <Customclass> (const Customclass & bn){ // works no problem
// do stuff
}
// if passing in c-string
template <>
void Myclass::operator + <char*> (const char* & my_string){ // compiler says doesn't work (without providing a different function declaration)
// do stuff
}
I assume the problem is related to the fact that I'm trying to pass a const reference to a c-string (char*
). Is there any way to do this?