4

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..

abhi_awake
  • 186
  • 1
  • 8
  • 1
    Look here (c, but the answer is valid here): https://stackoverflow.com/questions/890535/what-is-the-difference-between-char-const-and-const-char – krzaq Jun 10 '17 at 10:28
  • Eeerm you declared your template params as a reference (`T&`). So why do you wonder you have to keep it like that in the specialization? Can you elaborate about your doubts please? – πάντα ῥεῖ Jun 10 '17 at 10:34
  • Yes but why to use const& when u can use either 'const' or 'reference operator (&)' alone to make sure that pointer always point to the same address... – abhi_awake Jun 10 '17 at 10:35
  • @abhi_awake _"or 'reference operator (&)' alone"_ Huh, what?? – πάντα ῥεῖ Jun 10 '17 at 10:37
  • What i want to say is **const char* &a** or **const char* const a** says that a is read only i.e it points to same address and its content doesn't change but what does **const char* const& a** means.... Why here we have used both **const+reference operator (const&)** ??? – abhi_awake Jun 10 '17 at 10:40
  • 2
    `const char* &` is a non-const reference to `const char*`, not a const reference to `char*`. It is not a specialisation of `const T&`. (And you're using different types for the return value and the parameters.) – molbdnilo Jun 10 '17 at 10:44
  • thnkz @molbdnilo.... I got what was wrong... – abhi_awake Jun 10 '17 at 11:02

2 Answers2

2

In your first declaration

template<class T> const T& min(const T& a, const T& b) {...}

const T& is actually equivalent to T const &, i.e., a reference to a constant T. If you want to specialize that function, it still needs to get a reference to a constant something. If you want to specialize for const char* (which is the same as char const *, i.e., a pointer to a constant char) you thus need to write: char const * const &, i.e., a reference to a constant pointer to a constant char.

Generally, const always refers to whatever is written to its left, only if const itself is at the very beginning, it refers to whatever is written to its right.

Addendum (off-topic, this does not explain the actual problem):

Instead of template specialization, you could just overload min:

const char* min(const char* a, const char* b) {
    return (strcmp(a, b) < 0) ? a : b;
}
chtz
  • 17,329
  • 4
  • 26
  • 56
1

You actually have 2 different specializations for char*.

The signature of the 1st one

const char* const& min<const char*>(const char* &a,
const char* &b)

makes the value pointed to read only. While the 2nd one

const char* const& min<const char*>(const char* const& a,
const char* const& b)

also makes the pointer parameters being read only.
The 1st signature would allow to change the pointers passed as parameters from inside the function.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190