嵌套在 class 中的 C++11 枚举 class 的缩写语法?

Abbreviated syntax for a C++11 enum class nested in a class?

我使用一个 C++11 库,它有一个 header 和一个嵌套在 class 中的枚举 class:

class ClassWithARatherLongName
{
public:
    enum class EnumWithARatherLongName
    {
        VALUE1,
        VALUE2,
        VALUE3
    };
    (other code)
};

然后我在我的代码中使用枚举数:

void foo()
{
    ... ClassWithARatherLongName::EnumWithARatherLongName::VALUE1 ...
}

这可行,但很乏味。有没有办法简化语法?

理想情况下,我希望能够编写如下内容:

void foo()
{
    (some directive that allows to use an abbreviated syntax)
    ... VALUE1 ...
}

只需添加一个 typedef:

void foo()
{
    typedef ClassWithARatherLongName::EnumWithARatherLongName e;
    e::VALUE1
}

您可以使用命名空间来缩短名称。

您可以使用 typedefusing 创建 "shortcut":

using E = ClassWithARatherLongName::EnumWithARatherLongName;
E::VALUE1