+ 3

If I do not remove the "const", I receive an error massage: explicit specialization 'const char *.....' WHY?

const T min(const T x, const T y) { return ((x < y) ? x : y); } template<> const char* min(const char* s1, const char* s2) { return ((std::strcmp(s1, s2) < 0) ? s1 : s2); }

23rd Nov 2018, 9:19 PM
Petros Simidyan
Petros Simidyan - avatar
2 Antworten
+ 3
Because const char* is not the correct return type. The const in front of the T applies to the whole type, where T is const char*. const T would then be const char* const. ( a constant pointer that is pointing to a constant char ) Therefore the correct return type is const char* const. ---- Just as an FYI: Function overloading should be preferred over function specialization. http://www.gotw.ca/publications/mill17.htm
23rd Nov 2018, 10:36 PM
Dennis
Dennis - avatar
+ 2
Thanks for answer, now I've got it.
24th Nov 2018, 1:41 AM
Petros Simidyan
Petros Simidyan - avatar