ghost
ghost

Reputation: 41

How to convert a typedef enum into using enum in C++

I'm trying to explore "using" in C++, and currently I would like to convert typedef into using.

For example,

typedef enum tag_EnumType : int {
    A,
    B,
    C,
} EnumType;

And I tried to convert it as following

using EnumType = enum tag_EnumType : int {
    A,
    B,
    C,
};

However, the compilation failed. Could anyone help? Thanks in advance.

The truth is, I'm working on a giant project. Some of typedef enum could be rewritten as using, but others couldn't. Does anyone know the reason? Is it related with namespace or sth?

Upvotes: 3

Views: 619

Answers (1)

JVApen
JVApen

Reputation: 11317

typedef enum is a C way of creating enums. If you want it the C++ way, you better write:

enum EnumType : int {
    A,
    B,
    C,
};

In C++11 and above, scoped enums are also available, that way you prevent name collisions.

 enum class EnumType : int {
    A,
    B,
    C,
};

using statements are for other typedefs, some examples:

using IntVector = std::vector<int>;
using Iterator = IntVector::Iterator;
using FuncPtr = int(*)(char, double);

Upvotes: 2

Related Questions