符合标准的typedef我的枚举方式 [英] standards compliant way to typedef my enums

查看:150
本文介绍了符合标准的typedef我的枚举方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何正确地清除该枚举的范围,如何摆脱警告?符合标准的代码将与 foo :: bar :: mUpload 进行比较(请参阅这里),但显式的范围真的很长,并且使darn的东西不可读。



<可能还有另一种不使用typedef的方法?我不想修改枚举 - 我没有写它,它在其他地方使用。



警告C4482:使用非标准扩展:enum'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();
}


解决方案

一个类,你可以做的最好的就是把类放入你自己的范围,只需使用 class_name :: value 或定义类的typedef。在C ++ 03中,枚举的值是封闭范围的一部分(在你的情况下是类)。在C ++ 0x / 11中,您将能够使用枚举名称限定值:

  namespace first {namespace second { 
struct closure {
enum the_enum {one_value,another};
}
}}
使用第一::第二::封闭;
typedef first :: second :: encing the_enclosing;

assert(enclosureing :: one_value!= the_enclosing :: another);

将来,您的使用将是正确的(C ++ 11):

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


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

解决方案

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

这篇关于符合标准的typedef我的枚举方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆