I have the following code:
// string specializations
void foo(const char *a, const char *b);
void foo(const char *a, const std::string &b);
void foo(const std::string &a, const char *b);
void foo(const std::string &a, const std::string &b);
// generic implementation
template<typename TA, typename TB>
void foo(TA a, TA b)
{...}
The problem is that this test case:
char test[] = "test";
foo("test", test);
ends up calling the templated version of foo. Obviously, I can just add a few more overloads with various mixes of non-const parameters, but I want to know: is there's a better way to overload foo such that it is specialized on all const and non-const pairings of strings? One that doesn't require me to hope I haven't missed some permutation of argument types?
char *test = "test"isn't something you should do."test"isconst char*.std::string, or simply makestd::stringand require callers to convert"test"is of typeconst char[5], notconst char*."test"isconst char[5].const(and conversion toconst char*isn't a problem, while conversion tochar *is borderline and only possible because of a deprecated exception in the rules for type casting).