Hi,
I'm trying to get template specializations working for char * as well as for char[]. I.e. in the following code the last test does not use the specialization and fails:
Any hint, what I'm doing wrong?Code:#include <string> #include <iostream> #include <cstring> template<typename T1, typename T2> bool compare(const T1& lhs, const T2& rhs) { std::cout << "DEBUG: Using generic template\n"; return lhs == rhs; } template<> bool compare<const char*, const char*>(const char * const& lhs, const char * const& rhs) { std::cout << "DEBUG: Using template specialization\n"; return strcmp(lhs, rhs) == 0; } int main() { std::string test = "abc"; if (compare(test, test)) { std::cout << "Test 1 succeeded\n"; } if (compare(test.c_str(), test.c_str())) { std::cout << "Test 2 succeeded\n"; } if (compare(test.c_str(), "abc")) { std::cout << "Test 3 succeeded\n"; } return 0; }




Reply With Quote
