Tim Randall
Tim Randall

Reputation: 4155

Why put an enum in a struct and then use a typedef name?

I've found the following pattern used fairly commonly in our company's code.

struct Foo
{
    enum FType
    {
        TypeA,
        TypeB,
        Type_MAX
    };
};
typedef Foo::FType FooType;
[...]
FooType m_type;

My question is, what is the benefit of this? (Or, what problem does this avoid?) To be clear, I am wondering why they didn't just...

enum FooType
{
    TypeA,
    TypeB,
    Type_MAX
};
[...]
FooType m_type;

I can't ask the original programmer because they have been re-assigned, and it turns out that the designated subject matter expert in our company is, in fact, me.

If it helps, this code has been compiled at various times using many versions of MSVC, gcc, and clang for different target platforms... all pre C++11.

Can anyone see why this was done? Apologies in advance, if the answer turns out to be something trivial.

Edited to add: This is used inside classes. (When an enum is global, our style guide requires entries to begin with a common prefix, in order to distinguish them from other symbols.)

Upvotes: 2

Views: 1349

Answers (6)

Pete Becker
Pete Becker

Reputation: 76498

The perceived problem with plain old enums is that the enumerators become names in the scope where the enum is defined. As a result, you could say, for example,

FooType m_type;
m_type = TypeA;

If you had also defined a class names TypeA you'd have a conflict. Putting the enum inside a class means you have to use a scope qualifier to get at the name, which would remove the conflict. It also means you'd have to write

FooType m_type;
m_type = FooType::TypeA;

because the previous version wouldn't be valid.

A newer solution to this problem is scoped enums:

enum class FooType {
    TypeA,
    TypeB,
    Type_MAX
};

Now you can say

FooType m_type;
m_type = FooType::TypeA;

but not

m_type = TypeA;

One difference here, as @Jarod42 points out, is that an enumerator defined by a plain enum can be implicitly converted to int, while an enumerator from a scoped enum is not. So with the definition inside a class,

int i = FooType::TypeA;

is valid, and i gets the value 0. With a scoped enum it is not valid.

In both cases, a cast makes the conversion okay:

int i = static_cast<int>(FooType::TypeA);

Upvotes: 6

eerorika
eerorika

Reputation: 238461

My question is, what is the benefit of this? (Or, what problem does this avoid?)

It limits the individual enumeration identifiers into the namespace of the class:

int TypeA;

struct Foo
{
    enum FType
    {
        TypeA, // OK, Foo::TypeA does not conflict with ::TypeA
        TypeB,
        Type_MAX,
    };
};

enum FooType
{
    TypeA, // not OK, conflicts with the existing ::TypeA declaration
    TypeB,
    Type_MAX,
};

The programmer could have used a namespace instead, which might have been clearer.

Upvotes: 0

imreal
imreal

Reputation: 10378

You should always put enum's inside a struct (or a class) in pre-C++11. The reason for it is that it prevents pollution of the global namespace and it forces qualified names when using the members of the struct. i.e Foo::TypeA. Why? because otherwise someone else might decide to create a constant or another enum member named TypeA somewhere else in the code and there would be a name conflict.

The typedef is probably just for the convenience of not typing the fully qualified enum type name every time.

This only apply to pre C++11. C++11 has enum class which declares a scoped enum. You mentioned that this code was written before that.

Upvotes: 1

Sorin
Sorin

Reputation: 11968

In older C++ versions the enum names are added to the surrounding namespace.

In your second example I can do:

m_type = TypeA;

In the original example you need to do:

m_type = FooType::TypeA;

If TypeA is something fairly common in your application, I can see why you wouldn't want to pollute the namespace around it.

Upvotes: 1

V. Kravchenko
V. Kravchenko

Reputation: 1904

There probably wasn't enum class in C++ when this code was written, so that was the only solution to avoid outer namespace pollution.

Struct is used here more like a namespace. Still the author probably wanted name of enum itself to be in outer namespace, which is done by typedef.

Upvotes: 1

NathanOliver
NathanOliver

Reputation: 181027

Most likely this was done to prevent polluting the global scope with the enum members. When you have

enum FooType
{
    TypeA,
    TypeB,
    Type_MAX
};

TypeA, TypeB and Type_MAX become names in the global scope. This could cause conflicts with other enums or just other names already in use. By placing the enum in a struct you limit the names to being the the scope of the struct. Another way this is accomplished is using a namespace.

C++11 offers enum class that keeps the enum members scoped to the enum itself so this is no longer needed if you can deal with the more strict controls enum class imposes.

Upvotes: 4

Related Questions