Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the integer type used by an enum (C++)?

Tags:

c++

c

enums

If I have an C++ enum:

enum Foo {   Bar,   Baz,   Bork, }; 

How do I tell the compiler to use a uint16_t to actually store the value of the enum?

EDIT: Does GCC support this feature in its implementation of C++11?

like image 708
Linuxios Avatar asked Mar 24 '12 17:03

Linuxios


People also ask

Can we change value of enum in C?

You can change default values of enum elements during declaration (if necessary).

Is enum an integer in C?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

How do I change the value of an enum?

You can add a new value to a column of data type enum using ALTER MODIFY command. If you want the existing value of enum, then you need to manually write the existing enum value at the time of adding a new value to column of data type enum.

Is enum integer type?

In C#, an enum (or enumeration type) is used to assign constant names to a group of numeric integer values. It makes constant values more readable, for example, WeekDays.


1 Answers

You cannot do this in C++98/03. C++11 does allow you to do it, and without enum class the way everyone else seems to keep telling you:

enum EnumType : uint16_t {   Bar,   Baz,   Bork, }; 

Again, you don't have to use enum class. Not that it's a bad idea, but you don't have to.


Does GCC support this feature in its implementation of C++11?

Which version of GCC? It looks like GCC 4.4 added this functionality, but you should probably look to more recent versions, just for the sake of stability.

like image 85
Nicol Bolas Avatar answered Sep 25 '22 04:09

Nicol Bolas