There is such a pattern.

template<class T> T maxn(T *, int); 

and specialization for char*

 template<> char* maxn(char**, int); 

Now I would like to add also const , i.e. T maxn(const T *, int); , but after adding to the const specialization, an error is generated, i.e. maxn underlined in red.

 template<> char* maxn<char*>(const char**, int); 

Why? Who will explain?

  • Is this a function that returns the first argument? If so, it is highlighted due to the fact that you have the first constant argument, and you are trying to return a non-constant value. Add a const before the return value - Andrej Levkovitch
  • no, not the first argument - Samvel
  • in this case, demonstrate the function body - Andrej Levkovitch
  • 2
    So const char * should be everywhere in T , i.e. template<> const char * maxn< const char * >(const char * *, int); - VTT
  • There is not even a body. These are only prototypes. But I can write now. - Samvel

2 answers 2

The template specialization should correspond to the main template that it specializes.

First, you explicitly specified the template parameter T == char * in your specialization. But your first specialization has already been made for such a value of the parameter T Another specialization cannot be made for the same set of parameter values. (And why did you suddenly decide to explicitly indicate the value of the parameter, if in the first specialization you did not do this?)

Secondly, from the value of the template parameter T == char * immediately follows that the parameter of the function T * should be of type char ** . And you have written - const char ** . This is contrary to the main pattern.

Thirdly, it is clearly written in your main template that the type of the return value is related to the type of the parameter of the function ( T and T * respectively). This means that with the parameter type const char ** , the return value can in no way be char * . This also contradicts the main pattern.

In other words, your main template cannot have such a specialization.

Here is a specialization you can do

 template<> const char* maxn(const char**, int); // Она же // template<> const char* maxn<const char*>(const char**, int); 

but not what you have now.

And then everything depends on what you are trying to do. It is not clear from your question why you needed such a "specialization". Maybe you need an overload, not a specialization?

 // Два перегруженных шаблона template<class T> T maxn(T *, int); template<class T> T maxn(const T *, int); 

or

 // Шаблон и перегруженная функция template<class T> T maxn(T *, int); char* maxn(const char**, int); 
  • rather need an overload - AR Hovsepyan

Well, not having received a full answer here, I decided to ask the same question in English StackOverflow. And they answered me there

 template<> char* maxn<char*>(char* const *, int); 

Here is the link to the answer.