一个专门针对 const char *
的模板能否也捕捉到 char *
?
例如:
template <typename T> class Foo { /* ... */ };
template <> class Foo<const char *> { /* ... */ };
< p > Foo<char *>
会引用通用模板还是专用模板?< /p >
一个专门针对 const char *
的模板能否也捕捉到 char *
?
例如:
template <typename T> class Foo { /* ... */ };
template <> class Foo<const char *> { /* ... */ };
< p > Foo<char *>
会引用通用模板还是专用模板?< /p >
Foo<char*>
将引用泛型,因为char*
和const char*
是不同的类型。 对于函数来说,这更加令人困惑,因为有时候类型会添加引用符:const char*&
。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>
。
typename = void
,然后在部分特化中添加typename std::enable_if<std::is_same<T, char*>::value || std::is_same<T, const char*>::value>::type
。 - Collin Dauphinee"B"
。你是不是想把thing
变成一个char *
? - T.C.convertible
可能比is_same
的链式更好。 - Yakk - Adam Nevraumontchar*
和const char*
,但不是其他类型。也可能是你的方法。 - Mooing Duck