Rule A7-2-5 (advisory, design, non-automated)
Enumerations should be used to represent sets of related named constants.
Rationale
Explicit declaration of constants as an enumeration clearly shows that they are related, which enhances readability and maintenance. Note: Using switch statement on an enumeration is a common case and such an approach helps to detect errors, see M6-4-6.
Example
//% $Id: A7-2-5.cpp 305629 2018-01-29 13:29:25Z piotr.serwa $
#include <cstdint>
//compliant
enum class WebpageColors: std::uint32_t
{
Red,
Blue,
Green
};
//non-compliant
enum class Misc: std::uint32_t
{
Yellow,
Monday,
Holiday
};
See also
JSF December 2005 [8]: AV Rule 148: Enumeration types shall be used instead of integer types (and constants) to select from a limited series of choices. C++ Core Guidelines [11]: Enum.2: Use enumerations to represent sets of related named constants.