符合标准的方式来定义我的枚举

发布于 2024-09-10 05:37:34 字数 634 浏览 2 评论 0原文

如何在不正确明确枚举范围的情况下摆脱警告?符合标准的代码将与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

瑾夏年华 2024-09-17 05:37:34

如果枚举是在类中定义的,那么您能做的最好的事情就是将该类带入您自己的范围中,然后仅使用 class_name::value 或定义该类的 typedef。在 C++03 中,枚举的值是封闭范围的一部分(在您的情况下是类)。在 C++0x/11 中,您将能够使用枚举名称限定值:

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):

typedef first::second::enclosing::the_enum my_enum;
assert( my_enum::one_value != my_enum::another );

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 );

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 );
烟花肆意 2024-09-17 05:37:34

您可以将枚举封装到命名空间中,然后在该命名空间上使用 using 语句。

这显然只适用于类范围之外的枚举。

在您的情况下,我不明白您为什么不将其称为 bar::mUpload (在 using namespace foousing 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 (after using namespace foo, or using foo::bar)

征﹌骨岁月お 2024-09-17 05:37:34

您可以将 typedef 用于 foo::bar:

typedef foo::bar fb;
//...
fb::baz m = fb::mUpload;

或者您正在寻找不同的东西?

You can use a typedef for foo::bar:

typedef foo::bar fb;
//...
fb::baz m = fb::mUpload;

Or are you looking for something different?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文