问题描述
如何正确清除枚举,如何摆脱警告?符合标准的代码将与 foo :: bar :: mUpload
进行比较(请参阅),但是显式作用域真的很长,使得darn不可读。
How can I get rid of the warning, without explicitly scoping the enum properly? The standards-compliant code would be to compare against foo::bar::mUpload
(see here), but the explicit scopes are really long and make the darn thing unreadable.
也许有另一种方式,不使用typedef?我不想修改枚举 - 我没有写它和它在其他地方使用。
maybe there's another way that doesn't use typedef? i don't want to modify the enum--i didn't write it and its in use elsewhere.
警告C4482:非标准扩展使用:
warning C4482: nonstandard extension used: enum 'foo::bar::baz' used in qualified name
namespace foo {
class bar {
enum baz {mUpload = 0, mDownload};
}
}
typedef foo::bar::baz mode_t;
mode_t mode = getMode();
if (mode == mode_t::mUpload) //C4482
{
return uploadthingy();
}
else
{
assert(mode == mode_t::mDownload); //C4482
return downloadthingy();
}
推荐答案
一个类,你可以做的最好的方法是将类放入你自己的范围,只需使用
class_name :: value
或定义类的typedef。在C ++ 03中,枚举的值是包围范围的一部分(在您的情况下是类)。在C ++ 0x / 11中,您将能够使用枚举名称限定值:
If the enum is defined within a class, the best that you can do is bring the class into your own scope and just use
class_name::value
or define a typedef of the class. In C++03 the values of an enum are part of the enclosing scope (which in your case is the class). In C++0x/11 you will be able to qualify the values with the enum name:
namespace first { namespace second {
struct enclosing {
enum the_enum { one_value, another };
}
}}
using first::second::enclosing;
typedef first::second::enclosing the_enclosing;
assert( enclosing::one_value != the_enclosing::another );
以后,您的使用将是正确的(C ++ 11):
In the future, your usage will be correct (C++11):
typedef first::second::enclosing::the_enum my_enum;
assert( my_enum::one_value != my_enum::another );
这篇关于标准兼容的方式typedef我的枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!