专用于 const char * 的模板也会接受 char * 吗?

Will a template specialized for const char * accept char * as well?

专用于 const char * 的模板是否也适用于 char *

例如:

template <typename T> class Foo { /* ... */ };
template <> class Foo<const char *> { /* ... */ };

Foo<char *>指的是通用模板还是专用模板?

模板 classes 和函数仅与 完全匹配 匹配,因此在您的情况下,Foo<char*> 将引用泛型,因为 char*const char* 是不同的类型。这让函数变得更加混乱,因为有时引用会添加到类型中:const char*&.

制作一个接受指针变体的 class 模板有点复杂,但通常或多或少像这样工作:

template <typename T, typename allowed=void> class Foo { /* ... */ };

template <typename T> 
class Foo<T, typename std::enable_if<std::is_same<T, char*>::value || 
                           std::is_same<T, const char*>::value
                           >::type> { /* ... */ };

根据您的工作,您可能还需要 std::remove_reference<T>