Namespaces
Variants
Views
Actions

enumeration declaration

From cppreference.com
< cpp‎ | language
Revision as of 12:44, 24 March 2013 by Cubbi (Talk | contribs)

 
 
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function declaration
inline specifier
Exception specifications (deprecated)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
decltype (C++11)
auto (C++11)
alignas (C++11)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Implicit conversions - Explicit conversions
static_cast - dynamic_cast
const_cast - reinterpret_cast
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous
 

An enumeration is a distinct type whose value is restricted to one of several explicitly named constants ("enumerators"). The values of the constants are values of an integral type known as the underlying type of the enumeration.

enum Template:sparam : Template:sparam Template:sparam { Template:sparam = Template:sparam , Template:sparam = Template:sparam , ... } (1)
enum class Template:sparam : Template:sparam Template:sparam { Template:sparam = Template:sparam , Template:sparam = Template:sparam , ... } (2) (since C++11)
enum struct Template:sparam : Template:sparam Template:sparam { Template:sparam = Template:sparam , Template:sparam = Template:sparam , ... } (3) (since C++11)

1) declares an unscoped enumeration type. Each Template:sparam becomes accessible in the enclosing scope, and is implicitly-convertible to integral type, including bool. If not explicitly specified, the underlying type is an integral type capable of representing all enumerator values, which cannot be wider than int unless some Template:sparam evaluates to a constant that does not fit in an int

2-3) declares a scoped enumeration type. Each Template:sparam can only be accessed as Template:sparam. Conversion to integral types is possible with static_cast. If not explicitly specified, the underlying type is int.

Explanation

name - the name of the type declared by this declaration. An unscoped enumeration may be nameless, in which case it only introduces enumerator names as constants, but no new type
type(C++11) - optional integral type (any cv-qualification is ignored), used as the underlying type of the enumeration.
attr(C++11) - zero or more implementation-specific attributes of the form [[attribute]]
enumerator - zero or more enumerators which are introduced by this declaration. The names of the enumerators may be used anywhere constants are expected
constexpr - optional constant expression which evaluates to the value to be assigned to the enumerator. If it is omitted, the value is the value of the previous enumerator plus 1. If omitted for the first enumerator, the value is 0

Example

#include <iostream>
// color may be red (value 0), yellow (value 1), green (value 20), or blue (value 21)
enum color {
    red,
    yellow,
    green = 20,
    blue
};
// altitude may be altitude::high or altitude::low
enum class altitude : char { 
     high='h',
     low='l', // C++11 allows the extra comma
}; 
// the constant d is 0, the constant e is 1, the constant f is 3
enum { d, e, f=e+2 };
int main()
{
    color col = red;
    altitude a;
    a = altitude::low;
 
    std::cout << "red = " << col << " blue = " << blue << '\n'
              << "a = " << static_cast<char>(a) << '\n'
              << "f = " << f << '\n';
}

Output:

red = 0 blue = 21
a = l
f = 3