模板和显式特化

Templates and explicit specializations

我有模板

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

char*

的显式特化
template<> char* maxn(char**, int);

我想添加 const,

T maxn(const T *, int);

但是在显式特化中添加const后出现错误。

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

为什么?谁能给我解释一下?

P.S. Sorry for my English.))

给定参数类型 const T *constT 上是合格的。那么 char*(指向 char 的指针)应该是 char* constconst 指向 char 的指针),而不是 const char*(非指向 const char).

的常量指针
template<class T>
T maxn(const T *, int);

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

您应该为 const char*:

实例化方法
template<> const char* maxn<const char*>(const char**, int);

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

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

签名,所以你不能只在一个参数上添加const