在可能的类中使用C ++ 20中的“使用枚举"吗? [英] Use 'using enum' in C++20 in classes possible?

查看:52
本文介绍了在可能的类中使用C ++ 20中的“使用枚举"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此答案中,提到在即将到来的C ++ 20标准中可以使用 enum class 上>使用语句,然后将enum字段导入本地名称空间.

In this answer it was mentioned that in the upcoming C++20 standard it is possible to use the using statement on enum class and import the enum fields into the local namespace.

我想知道这是否还意味着我也可以在这样的类定义中使用它:

I was wondering if that also means that I can also use it within class definitions like this:

class Foo {
    enum class Color
    {
        red, 
        blue
    };
    using enum Color;
};

int main()
{
    Foo::Color c = Foo::red;
}

还是我仍然需要提供完整的名称空间?:

Or do I still need to give the full namespace?:

    Foo::Color c = Foo::Color::red;

我在wandbox.org中尝试过,但是似乎gcc和clang都不知道使用枚举.

I tried it in wandbox.org, but it seems that neither gcc nor clang know about using enum yet.

推荐答案

是的, Foo :: Red 可以正常工作.使用枚举E 的行为源自 [enum.udecl] :

Yes, Foo::Red will work fine. using enum E behaves as, from [enum.udecl]:

一个 using-enum-declaration 引入命名枚举的枚举器名称,就像每个枚举器的 using-declaration 一样.

A using-enum-declaration introduces the enumerator names of the named enumeration as if by a using-declaration for each enumerator.

标准中包含一个确切说明这种情况的示例:

And the standard contains an example of exactly this case:

[注意:类范围中的 using-enum-declaration 将命名枚举的枚举数作为成员添加到范围.这意味着它们可用于成员查找.[示例:

[ Note: A using-enum-declaration in class scope adds the enumerators of the named enumeration as members to the scope. This means they are accessible for member lookup. [ Example:

enum class fruit { orange, apple };
struct S {
  using enum fruit;             // OK, introduces orange and apple into S
};
void f() {
  S s;
  s.orange;                     // OK, names fruit​::​orange
  S::orange;                    // OK, names fruit​::​orange
}

-结束示例] -结束注释]


但是请注意,此功能的特定拼写存在争议. enum E 是一种详尽的类型说明符(很像 class C struct S ).通常,精心设计的类型说明符的行为与其基础版本完全相同.详细说明只是为了消除歧义,您几乎不需要消除歧义,因此您不会经常看到它.但是,在这种特殊情况下,使用enum E 和使用E 的实际上意味着完全不同且完全不相关的事物.因此请记住,尽管当前正在起草工作中,甚至已经发布在CD中,但该功能仍有可能无法真正实现C ++ 20.因此,除非编译器确定有必要实施,否则不太可能会实现此功能(对于编译器作者来说,C ++ 20确实不缺乏工作...)


Note however that there is some controversy around the particular spelling for this feature. enum E is what's known as an elaborated type specifier (much like class C or struct S). Typically, elaborated type specifiers behave exactly the same was as their underlying versions. Elaborating is just meant to disambiguate, and you rarely need to disambiguate, so you wouldn't see it very often. However, in this particular case, using enum E and using E actually mean wildly different and wholly unrelated things. So keep in mind that there is a chance that this feature may not yet actually make C++20, despite currently being in the working draft and even having been published in the CD. As such, it's unlikely that compilers will implement this feature until they're sure it's necessary to implement (C++20 isn't exactly lacking in work for compiler writers...)

这篇关于在可能的类中使用C ++ 20中的“使用枚举"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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