Skip to main content

Posts

Showing posts with the label enum

[C++] enum class

Traditional C++ enum had several issues. To solve these problems, C++11 introduced a new feature called enum class . In this article, I will examine the problems with the traditional enum and how they are solved with enum class . First, traditional enum could not be forward-declared. The reason was that if the values in the enumerator were unknown, it was impossible to determine their size . However, enum class is treated as int if an underlying type is not specified, assigning values outside the range of an int will raise a compilation error. If you want to use values outside the range of an int , you need to specify the underlying type. Another problem with traditional enum was that the scope of enumerator names was not limited. Let's see the following example. Here, we try to represent the results of IO and Parse functions with enum s. However, this code will not compile because the Error and Ok of IOResult conflict with those of ParseResult . To resolve t...