Understanding how to correct declare variables is important in programming, especially in a modern C++ Application. There is an enumeration method that allows you to easily enumerate (count or determine their place in an order) values in variables. In C++, enumeration is very important and widely used. In C++, enumeration can be done with an enum keyword which can be used in unscoped and scoped enumerations. There are some subtleties to declaring and using an enum
especially if you use forward declaration of the enum
. In this post, we explain what an enum is, what a forward declaration is, and how to declare a forward enum
Enumeration in Modern C++ along with some easy-to-understand examples.
Table of Contents
What is (enum) enumeration in Modern C++?
An enumeration is used to declare constants in a range of values easily. 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. An enum
can be used in two ways in C++. These are:
- Unscoped enumeration
- Scoped enumeration (strongly typed enums)
Now, let’s see very simple some enum examples.
1 2 |
enum class material { wood, metal, glass } ; // wood = 0, metal = 1, glass = 2 material t = material::wood; //using material:: scope |
or you can explicitly declare their values too:
1 2 |
enum class difficulty:unsigned char { easy= 10, modarate = 20, hard = 30 } ; difficulty cpp = difficulty::easy; |
What is a forward declaration enum enumeration in Modern C++?
C++11 was the first standard to introduce forward declaration of an enum
. We can declare an enumeration without providing a list of enumerators. Such declarations would not be definitions and can be provided only for enumerations with fixed underlying types. Here is a simple example.
1 2 |
// Forward Declaration enum enum class my_enum; |
An enumeration can then be re-declared, possibly providing the missing list of enumerators, but the re-declaration must match the previous declaration. This feature is one of the C++11 features added to CLANG compilers like BCC32c, and other compiler like BCC32 etc.
Is there an example of a forward declaration enum enumeration in Modern C++?
Here is a simple example that shows how you use a forward declaration enum, how to define an enum, and how you can use it.
1 2 3 4 5 6 7 8 9 10 |
// Forward Declaration enum enum class my_enum; // enum Definition enum class my_enum { x, y, z}; int main() { const my_enum num = my_enum::x; } |
When we use forward declaration enum, it must specify (implicitly or explicitly) the underlying size of the enum
. Here are some examples,
1 2 3 |
enum E : short; // OK: unscoped, underlying type is short enum class G : short; // OK: scoped, underlying type is short enum class H; // OK: scoped, underlying type is int |
Here are some more forward declaration enum
examples that are acceptable.
1 2 3 4 |
enum E : short; // OK: redeclaration of E enum class G : short; // OK: redeclaration of G enum class H; // OK: redeclaration of H enum class I : int; // OK: redeclaration of I |
Here are some invalid enum usage examples.
1 2 3 4 5 6 7 |
enum class E : short; // illegal: previously declared as unscoped enum F: // illegal: enum-base is required enum G : short; // illegal: previously declared as scoped enum E; // illegal: enum-base is required enum E : int; // illegal: different underlying type enum class G; // illegal: different underlying type enum class H : short; // illegal: different underlying type |
Is there a full example of forward declaration enum enumeration in modern C++?
Here is a full forward declaration of enum
example in C++.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> // Forward Declaration enum enum class my_enum; // enum Definition enum class my_enum { x, y, z}; int main() { const my_enum num = my_enum::x; if( num == my_enum::x) { std::cout << "num is equal to my_enum::x\n"; } system("pause"); return 0; } |
Forward declaration for an enum
is a feature supported by both BCC32 and the Clang-enhanced C++ compilers. For more information about this feature, see Forward Declaration of Enums (C++11).
These are other enumeration examples. These are explained in other articles of LearnCPlusPlus.org. Use this dynamic search to find out more information on how to declare and use enum enumeration types in C++: https://learncplusplus.org/?s=enum
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.