我正在尝试实现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
发布于 2012-07-04 02:28:11
这
template<enum E>
承诺模板参数是enum E
类型的值。参数不是类型(类型模板参数是由typename
引入的,或者为了向后兼容,请使用class
。甚至不允许使用struct
)。就像是在说
template<int i>
除了没有给出变量的名称。
从那时起,事情就变得糟糕了。
发布于 2012-07-04 02:29:42
你的问题是
template<enum E>
被解释为未命名参数,其类型为forward声明了名为E
的枚举。
在语义上与
template<int>
只需用enum E
替换int
。
https://stackoverflow.com/questions/11316912
复制相似问题