C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

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:

  • 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.

Here is an unscoped enumeration example.

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.

Here is a strongly typed (scoped enumeration) example.

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.

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.

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

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.

What's The BIG Difference Between Enum And Strongly Typed Enum In Modern C++? The C++ Builder Logo

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?