user1387866
user1387866

Reputation: 2984

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

I use a C++11 library which has a header with an enum class nested in a class:

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

Then I use the enumerators in my code:

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

This works, but is tedious. Isn't there a way to abbreviate the syntax?

Ideally, I would like to be able to write something like:

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

Upvotes: 1

Views: 690

Answers (2)

m.s.
m.s.

Reputation: 16324

You can use a typedef or using to create a "shortcut":

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

Upvotes: 3

Klemens Morgenstern
Klemens Morgenstern

Reputation: 832

Just add a typedef:

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

You might be able to shorten the names, by using namespaces.

Upvotes: 1

Related Questions