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++?
Table of Contents
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:
- Unscoped enumeration (enum)
- Scoped enumeration (strongly typed enum)
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.
1 |
enum name(optional) { enumarator = constexpr, enumarator = constexpr, ... } |
Here is an unscoped enumeration example.
1 2 |
enum material { wood, metal, glass } ; // wood = 0, metal = 1, glass = 2 material t = wood; |
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.
1 |
enum <struct|class name> { enumerator = constexpr , enumerator = constexpr , ... } |
Here is a strongly typed (scoped enumeration) example.
1 2 |
enum class material { wood, metal, glass } ; // wood = 0, metal = 1, glass = 2 material t = material::wood; //using material:: scope |
The strongly typed enums (scoped enums) solve most of the limitations incurred by old-style enums and have some significant differences.
- Complete Type Safety: You can get type safety by disallowing all implicit conversions of scoped enums to other types.
- Scope Issues: 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.
- Well-defined Underlying Type: You can get a new scope, and the enum is not anymore in the enclosing scope, saving itself from name conflicts.
- Forward Declaration: Any enum with a fixed underlying type can be forward declared.
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.
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> int main() { // old style e-num enum table { wood, metal, glass } ; // strongly typed enum enum class door { wood, metal, glass } ; } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> int main() { // old style e-num enum door { wood, metal, glass } ; enum table { wood, metal, glass } ; // ERROR! // strongly typed enum enum class door2 { wood, metal, glass } ; enum class table2 { wood, metal, glass } ; // No Error! } |
The old-style enum
implicitly converts to integral types, which may not be used with some operations. Here is an example.
1 2 3 4 5 6 7 |
#include <iostream> int main() { enum door { wood, metal, glass } ; bool b = glass && metal; // not logical } |
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.
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> int main() { // unscoped enum enum door : char { wood, metal, glass } ; // strongly typed enum enum class table : char { wood, metal, glass } ; } |
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition