I have a problem with the enum class feature of c++11. A minimal code example is:
我对c++11的enum类特性有一个问题。一个最小的代码示例是:
template<typename T>
class AClass{
public:
enum class paramNames{
PA = 0,
PB,
PC,
NUM
};
private:
double params[paramNames::NUM];
}
when I want to compile a program, which uses this program with gcc I get the following error message:
当我想要编译一个程序,它使用这个程序与gcc时,我得到以下错误信息:
error: 'paramNames' is not a class or namespace double params[paramNames::NUM];
错误:“paramNames”不是类或名称空间双参数[paramNames: NUM];
I would appreciate, if someone can explain to me, how to use the new enum class feature correctly.
如果有人能向我解释如何正确地使用enum类特性,我将不胜感激。
1 个解决方案
#1
4
Unlike the old enum
there is no implicit conversion to int
. By design enum class
cannot be converted to the underlying type implicitly. You can read more about the justification for that here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf. To do what you want you need to use a static cast and do something like this:
与旧的enum不同,它没有对int的隐式转换。通过设计enum类不能隐式地转换为底层类型。您可以在这里阅读更多的理由:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf。要做你想做的事,你需要使用静态石膏,并做如下的事情:
template<typename T>
class AClass{
public:
enum class paramNames: int{//specify the underlying type
PA = 0,
PB,
PC,
NUM
};
private:
double params[static_cast<int>(paramNames::NUM)];
}
EDIT: Make sure your compiler has the c++11 language features available to it, otherwise you will get errors doing this.
编辑:确保你的编译器有c++11语言的特性,否则你会在这方面出错。
#1
4
Unlike the old enum
there is no implicit conversion to int
. By design enum class
cannot be converted to the underlying type implicitly. You can read more about the justification for that here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf. To do what you want you need to use a static cast and do something like this:
与旧的enum不同,它没有对int的隐式转换。通过设计enum类不能隐式地转换为底层类型。您可以在这里阅读更多的理由:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2347.pdf。要做你想做的事,你需要使用静态石膏,并做如下的事情:
template<typename T>
class AClass{
public:
enum class paramNames: int{//specify the underlying type
PA = 0,
PB,
PC,
NUM
};
private:
double params[static_cast<int>(paramNames::NUM)];
}
EDIT: Make sure your compiler has the c++11 language features available to it, otherwise you will get errors doing this.
编辑:确保你的编译器有c++11语言的特性,否则你会在这方面出错。