符合标准的方式来定义我的枚举
如何在不正确明确枚举范围的情况下摆脱警告?符合标准的代码将与 foo::bar::mUpload
进行比较(请参阅这里),但是显式作用域真的很长,使得该死的东西不可读。
也许还有另一种不使用 typedef 的方法?我不想修改枚举——我没有编写它并且它在其他地方使用。
警告 C4482:使用了非标准扩展:在限定名称中使用了枚举“foo::bar::baz”
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();
}
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.
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.
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果枚举是在类中定义的,那么您能做的最好的事情就是将该类带入您自己的范围中,然后仅使用
class_name::value
或定义该类的 typedef。在 C++03 中,枚举的值是封闭范围的一部分(在您的情况下是类)。在 C++0x/11 中,您将能够使用枚举名称限定值:将来,您的用法将是正确的 (C++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:In the future, your usage will be correct (C++11):
您可以将枚举封装到命名空间中,然后在该命名空间上使用
using
语句。这显然只适用于类范围之外的枚举。
在您的情况下,我不明白您为什么不将其称为
bar::mUpload
(在using namespace foo
或using foo::bar 之后)
You can enclose your enum into a namespace, and then use a
using
statement on that namespace.This obviously only works for enum's outside of class scope.
In your case I don't see why don't you refer to it as
bar::mUpload
(afterusing namespace foo
, orusing foo::bar
)您可以将 typedef 用于 foo::bar:
或者您正在寻找不同的东西?
You can use a typedef for foo::bar:
Or are you looking for something different?