一种使用C ++ 0x枚举类中所有不合格名称的方法? [英] A way to use all the unqualified names in a C++0x enum class?

查看:132
本文介绍了一种使用C ++ 0x枚举类中所有不合格名称的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的C ++(C ++ 0x或C ++ 11)有一种新的枚举,一个枚举类,其名称被限定到枚举(除其他外)。

 枚举类E {
VAL1,VAL2
};

void fun(){
E e = E :: VAL1; //合格的名字
}

然而,我想知道如果我可以选择使用在一定范围内不合格的名称。如下:

  void fun(){
using E :: *;
E e = VAL1;
switch(e){
case VAL2:...

我看到我可以使用E :: VAL1 编写并获取一个值。但是我不想为更大的枚举的每个值都这样做。

解决方案

C ++ 11中没有办法做到这一点。为了防止这种情况你不知道 - 即使没有这个枚举,你也可以得到 E :: Val1 符号。对于这样的枚举,您可以使用 E :: 访问 Val1



但是,您不能采用范围枚举,并有选择地使其所有枚举器在给定范围内可见。还应该注意的是,您可以使用E :: Val1 编写 。该规范明确禁止这一点,您的编译器不会拒绝它。


The new C++ (C++0x or C++11) has an new kind of enum, an "enum class" where the names are scoped to the enum (among other things).

enum class E {
    VAL1, VAL2
};

void fun() {
    E e = E::VAL1;  // Qualified name
}

I'm wondering, however, if I can selectively use the unqualified name in a certain scope. Something like:

void fun() {
    using E::*;
    E e = VAL1;
    switch (e) {
        case VAL2: ...

I see I can write using E::VAL1 and get one value. But I don't want to do that for every value of a larger enum.

解决方案

There is no way to do this in C++11. Just in case you are not aware of it - you get the E::Val1 notation even for an unscoped enumeration. For such an enumeration, you have Val1 accessible with and without the use of E::.

But you cannot take a scoped enumeration and selectively make all its enumerators visible in a given scope. It should also be noted that you can not write using E::Val1. The spec explicitly forbids this, your compiler just doesn't reject it yet.

这篇关于一种使用C ++ 0x枚举类中所有不合格名称的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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