Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum vs Strongly typed enum

Tags:

c++

enums

c++11

I am a beginner in C++ programming.

Today I come across a new topic: strongly typed enum. I've researched it a bit but till now I am unable to find out why do we need this and what is the use of the same?

For example if we have:

enum xyz{a, b, c}; /*a = 0, b = 1, c = 2, (Typical C format)*/ 

Why do we need to write:

enum class xyz{a, b, c}; 

What are we trying to do here? My most important doubt is how to use it. Could you provide a small example, which will make me understand.

like image 478
Rasmi Ranjan Nayak Avatar asked Sep 25 '12 10:09

Rasmi Ranjan Nayak


People also ask

Are enums strongly-typed?

Enum Class C++11 has introduced enum classes (also called scoped enumerations), that makes enumerations both strongly typed and strongly scoped.

What is difference between enum and enum class?

An enum just spills its contents into the enclosing scope, and is basically a const static integer. This means that the first element of any default enum is the same using the == operator. Enum classes have their own scope, and don't pollute the namespace that they are in.

Is enum class or data type?

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).

What is enum class in C++?

An enumeration is a user-defined type that consists of a set of named integral constants that are known as enumerators. This article covers the ISO Standard C++ Language enum type and the scoped (or strongly-typed) enum class type which is introduced in C++11.


2 Answers

OK, first example: old-style enums do not have their own scope:

enum Animals {Bear, Cat, Chicken}; enum Birds {Eagle, Duck, Chicken}; // error! Chicken has already been declared!  enum class Fruits { Apple, Pear, Orange }; enum class Colours { Blue, White, Orange }; // no problem! 

Second, they implicitly convert to integral types, which can lead to strange behaviour:

bool b = Bear && Duck; // what? 

Finally, you can specify the underlying integral type of C++11 enums:

enum class Foo : char { A, B, C}; 

Previously, the underlying type was not specified, which could cause compatibility problems between platforms. Edit It has been pointed out in comments that you can also specify the underlying integral type of an "old style" enum in C++11.

like image 50
juanchopanza Avatar answered Sep 21 '22 04:09

juanchopanza


There's a good article about enums at this IBM page, it's very detailed and well-written. Here are some important points in a nutshell:

The scoped enums solve most of the limitations incurred by regular enums: complete type safety, well-defined underlying type, scope issues, and forward declaration.

  • You get type safety by disallowing all implicit conversions of scoped enums to other types.
  • You get a new scope, and the enum is not anymore in the enclosing scope, saving itself from name conflicts.
  • Scoped enums gives you the ability to specify the underlying type of the enumeration, and for scoped enums, it defaults to int if you choose not to specify it.
  • Any enum with a fixed underlying type can be forward declared.
like image 25
SingerOfTheFall Avatar answered Sep 18 '22 04:09

SingerOfTheFall