首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >is_enum实现

is_enum实现
EN

Stack Overflow用户
提问于 2012-07-04 02:24:22
回答 2查看 5.2K关注 0票数 20

我正在尝试实现std::is_enum。到目前为止,我的代码如下:

template<typename T>
struct is_enum {
    static bool value;
};

template<typename T>
bool is_enum<T>::value = false;

template<enum E>
struct is_enum {
    static bool value;
};

template<enum E>
bool is_enum<E>::value = true;

此代码会导致错误。更准确地说:

g++ -std=c++0x -Wall -o "enum2" "enum2.cpp" (in directory: /home/aristophanes/Desktop/C++)
Compilation failed.
enum2.cpp:11:15: error: use of enum ‘E’ without previous declaration
enum2.cpp:3:10: error: template parameter ‘class T’
enum2.cpp:12:8: error: redeclared here as ‘int E’
enum2.cpp:16:15: error: use of enum ‘E’ without previous declaration
enum2.cpp:17:14: error: ‘E’ was not declared in this scope
enum2.cpp:17:15: error: template argument 1 is invalid
enum2.cpp:17:18: error: template declaration of ‘bool value’

谁能给我解释一下我哪里弄错了?是我的错还是编译器的错?提前谢谢。

编辑:如果它是完全错误的,那么我如何纠正它?

注意:我使用的是g++ -o <file> <file>.cpp

EN

回答 2

Stack Overflow用户

发布于 2012-07-04 02:28:11

template<enum E>

承诺模板参数是enum E类型的值。参数不是类型(类型模板参数是由typename引入的,或者为了向后兼容,请使用class。甚至不允许使用struct )。就像是在说

template<int i>

除了没有给出变量的名称。

从那时起,事情就变得糟糕了。

票数 9
EN

Stack Overflow用户

发布于 2012-07-04 02:29:42

你的问题是

template<enum E>

被解释为未命名参数,其类型为forward声明了名为E的枚举。

在语义上与

template<int>

只需用enum E替换int

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11316912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档