gpt4 book ai didi

c++ - 专门用于枚举的模板

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:56:59 26 4
gpt4 key购买 nike

谁能告诉我为什么这不起作用?

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com