作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
谁能告诉我为什么这不起作用?
enum CompCriteria{ByKey,ByValue,ByeKeyAndValue};
template<class T>
struct X;
template<>
struct X<CompCriteria>
{
};
int _tmain(int argc, _TCHAR* argv[])
{
X<CompCriteria::ByeKeyAndValue> x;
return 0;
}
最佳答案
您将参数化类型 和参数化值 混为一谈。模板参数可以是类型或常量。例如:
template <typename T>
struct Foo;
相对于..
template <int N>
struct Foo;
看起来您想根据枚举常量 而非类型来专门化您的模板。意思是,你需要说:
enum CompCriteria{ByKey,ByValue,ByeKeyAndValue};
template<CompCriteria>
struct X;
// Specialization for ByKeyAndValue
//
template<>
struct X<ByeKeyAndValue>
{
};
int main()
{
X<ByeKeyAndValue> x; // instantiate specialization
return 0;
}
此外,您不能使用 namespace
运算符引用枚举。如果你想封装你的枚举,你需要将它们包装在一个命名空间中:
namespace CompCriteria
{
enum type {ByKey,ByValue,ByeKeyAndValue};
}
关于c++ - 专门用于枚举的模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4238710/
我是一名优秀的程序员,十分优秀!