Site icon Learn C++

What’s The BIG Difference Between Enum And Strongly Typed Enum In Modern C++?

Whats The BIG Difference Between Enum And Strongly Typed Enum In Modern C++

Variable declaration is important in programming and there is enumeration method that allows you to easily enumerate values in variables. In C++, enumeration is very important and widely used in different syntaxes. In C++, enumeration can be done with enum keyword which can be used as in unscoped and scoped enumerations. C++ is a great programming language that has many options to enumerate variables wisely. C++11 and above has modern enumeration methods that can be used in a modern C++ Editor and compiler. In this post, we explain what is the big difference between enum and strongly typed enum in modern C++ and why it matters.

First of all, let’s learn what is enumeration in programming, and which types we can use in C++?

What is the difference between enum vs strongly typed enum in modern C++?

An enumeration is important in programming, used to declare constants in a range of values easily that it may include several explicitly named constants which are also called as enumerators. In C++, enumeration can be done with the enum keyword which is also known as enum-key. The enum keyword can be used in two ways in C++. These are:

In this article we will explain the difference between enum and strongly typed enum.

How to use enum in unscoped enumeration in C++?

Unscoped enumeration is an old enumeration style, used before C++11 and improved after C++11 standards. In this enumeration type, the enum keyword can be used to declare an unscoped enumeration type whose fundamental type is fixed, not fixed, or can be used to specify the name and the type. For example, here is an enum syntax for the unscoped enumeration type whose type is not fixed.

[crayon-66381da483fa4642821049/]

Here is an unscoped enumeration example.

[crayon-66381da483fad864548970/]

How to use strongly typed enums in modern in C++?


Scoped enumeration comes with the C++11 standards. In this enumeration type, the enum keyword is used with a class or struct to declare a scoped enumeration type. Its fundamental type can be fixed, not fixed, or can be used to specify the name and the type. For example, here is an enum syntax for the scoped enumeration type whose type is not fixed.

[crayon-66381da483faf957128238/]

Here is a strongly typed (scoped enumeration) example.

[crayon-66381da483fb1418277120/]

The strongly typed enums (scoped enums) solve most of the limitations incurred by old-style enums and have some significant differences.

Is there an example of the difference between enum and strongly typed enum in modern C++?

The regular enum enumerators have no scope, they have no class or struct, but a strongly typed enum has a scope struct or class, this is why they are called scoped enum. Here is an example.

[crayon-66381da483fb3644197016/]

The old-style enum does not have its own scope and they have problems when they have same members. But you can declare many strongly type enums with the same members. This is shown in the example below.

[crayon-66381da483fb7681109672/]

The old-style enum implicitly converts to integral types, which may not be used with some operations. Here is an example.

[crayon-66381da483fb9798117227/]

The old-style enum has no type in declaration until C++11. The new unscoped enum use strongly typed enum, we can specify the underlying integral type of C++11 enums as below.

[crayon-66381da483fba004097315/]

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

Exit mobile version