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.
or you can explicitly declare their values too:
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.
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.
When we use forward declaration enum, it must specify (implicitly or explicitly) the underlying size of the enum
. Here are some examples,
Here are some more forward declaration enum
examples that are acceptable.
Here are some invalid enum usage examples.
Is there a full example of forward declaration enum enumeration in modern C++?
Here is a full forward declaration of enum
example in C++.
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.