Quick Q: Can an enum class be converted to the underlying type?—StackOverflow

Save to:
Instapaper Pocket Readability

Quick A: Yes, but it requires an explicit cast. One of the key differences between enum and enum class: the latter has no implicit conversion to the underlying type.

Can an enum class be converted to the underlying type?

Is there a way to convert an enum class field to the underlying type? I thought this would be automatic, but apparently not.

enum class my_fields : unsigned { field = 1 };

unsigned a = my_fields::field;

That assignment is being rejected by GCC. error: cannot convert 'my_fields' to 'unsigned int' in assignment.

Add a Comment

You must sign in or register to add a comment.

Comments (0)

There are currently no comments on this entry.