在我的模板化函数中,我试图检查类型 T 是否属于特定类型。我该怎么做?
p/s 我知道模板规范方式,但我不想那样做。
template<class T> int foo(T a) {
// check if T of type, say, String?
}
谢谢!
最佳答案
使用特化来代替检查类型。否则,请勿使用模板。
template<class T> int foo(T a) {
// generic implementation
}
template<> int foo(SpecialType a) {
// will be selected by compiler
}
SpecialType x;
OtherType y;
foo(x); // calls second, specialized version
foo(y); // calls generic version
关于c++ - 如何检查我的模板类是否属于特定类类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31251399/