This is my function template
template<class T> const T& min(const T& a, const T& b) {
return (a < b) ? a : b;
}
The sole purpose of function is to return min of two argument. The arguments are read only..
Here is explicit specialization for char* arguments
//Code 1:
template<>
const char*& min<const char*>(const char* &a,
const char* &b) {
return (strcmp(a, b) < 0) ? a : b;
}
Even though having read only argument this code gives an error. Although code below works perfectly
//Code 2:
template<>
const char* const& min<const char*>(const char* const& a,
const char* const& b) {
return (strcmp(a, b) < 0) ? a : b;
}
Why do i have to provide const& after char* if & or const alone is enough for making argument read only. What is the meaning of const& in Code 2..??
EDITED:
I am getting this error code while compiling with Code 1..
error: template-id 'min<const char*>' for 'const char* const& min(const char*&, const char*&)' does not match any template declaration|
while there is no error code with compiling with Code 2..