0

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?

Ben
  • 149
  • 1
  • 9
  • 6
    When `T` is `char*`, the argument of the template is `char * const`, not `const char*`. Leading const is (again) misleading. You want a `` specialization (whose argument has to be `char const * const`. – StoryTeller - Unslander Monica Oct 22 '20 at 10:22
  • @StoryTeller-UnslanderMonica So based on what I think you're telling me, I've changed the specialization for `char*` as an argument to `template <> void Myclass::operator + (char const * const & my_c_string){ //do stuff }`. I still get compile error saying "Undefined symbol: void Myclass::operator+(char const (&) [8])". The whole point of my question is how to write the specialization so that it takes a c-string, but still works with the template declaration of `template void operator + (const T & any_type);` – Ben Oct 22 '20 at 17:35
  • That error is unrelated to the issue my comment addresses. That one is due to the nature of templates and string literals (each literal has an array type for which a different specialization taking an array reference is instantiated). As you've learned, specialization as a customization point is a whole can of worms (https://stackoverflow.com/questions/7108033/template-specialization-vs-function-overloading). It would be better for you to provide the overloads. You can always create a template in the cpp file (give it internal linkage) and have it do the actual work. – StoryTeller - Unslander Monica Oct 22 '20 at 17:57
  • I had started with several overloads, but was hoping to simplify the code with some templates. For example, I had six other argument types that worked seamlessly with the single template declaration, and then the nasty c-string came along and messed it up. – Ben Oct 23 '20 at 07:23

0 Answers0