-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathenumeration.html
More file actions
181 lines (159 loc) · 41 KB
/
enumeration.html
File metadata and controls
181 lines (159 loc) · 41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>C++ | Enumeration</title>
<meta name="generator" content="VuePress 1.8.2">
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/site.webmanifest">
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png">
<link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5">
<meta name="description" content="Iteration over an enum, Basic Enumeration Declaration, Enumeration in switch statements, Scoped enums, Enum forward declaration in C++11">
<meta property="og:site_name" content="DevTut">
<meta property="og:title" content="C++ | Enumeration">
<meta property="og:description" content="Iteration over an enum, Basic Enumeration Declaration, Enumeration in switch statements, Scoped enums, Enum forward declaration in C++11">
<meta property="og:type" content="article">
<meta property="og:url" content="/cpp/enumeration.html">
<meta property="og:image" content="/logo.png">
<meta name="twitter:title" content="C++ | Enumeration">
<meta name="twitter:description" content="Iteration over an enum, Basic Enumeration Declaration, Enumeration in switch statements, Scoped enums, Enum forward declaration in C++11">
<meta name="twitter:url" content="/cpp/enumeration.html">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="/logo.png">
<meta name="theme-color" content="#ffffff">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="msapplication-TileImage" content="/mstile-150x150.png">
<meta name="msapplication-TileColor" content="#da532c">
<meta name="google-site-verification" content="76_rKXgwMVIjd-axJC_1zPV9OS4mEjvtgjYOWVkAdnQ">
<link rel="preload" href="/assets/css/0.styles.60619e34.css" as="style"><link rel="preload" href="/assets/js/app.1779e102.js" as="script"><link rel="preload" href="/assets/js/3.2cfa8016.js" as="script"><link rel="preload" href="/assets/js/648.b9166d4d.js" as="script">
<link rel="stylesheet" href="/assets/css/0.styles.60619e34.css">
</head>
<body>
<div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">DevTut</span></a> <div class="links"><form id="search-form" role="search" class="algolia-search-wrapper search-box"><input id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"> <a href="https://github.com/devtut/generate" target="_blank" rel="noopener noreferrer" class="repo-link">
GitHub
<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>C++</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/cpp/" aria-current="page" class="sidebar-link">Disclaimer</a></li><li><a href="/cpp/getting-started-with-cpp.html" class="sidebar-link">Getting started with C++</a></li><li><a href="/cpp/literals.html" class="sidebar-link">Literals</a></li><li><a href="/cpp/operator-precedence.html" class="sidebar-link">operator precedence</a></li><li><a href="/cpp/floating-point-arithmetic.html" class="sidebar-link">Floating Point Arithmetic</a></li><li><a href="/cpp/bit-operators.html" class="sidebar-link">Bit Operators</a></li><li><a href="/cpp/bit-manipulation.html" class="sidebar-link">Bit Manipulation</a></li><li><a href="/cpp/bit-fields.html" class="sidebar-link">Bit fields</a></li><li><a href="/cpp/arrays.html" class="sidebar-link">Arrays</a></li><li><a href="/cpp/iterators.html" class="sidebar-link">Iterators</a></li><li><a href="/cpp/basic-input-output-in-c.html" class="sidebar-link">Basic input/output in c++</a></li><li><a href="/cpp/loops.html" class="sidebar-link">Loops</a></li><li><a href="/cpp/file-i-o.html" class="sidebar-link">File I/O</a></li><li><a href="/cpp/cpp-streams.html" class="sidebar-link">C++ Streams</a></li><li><a href="/cpp/stream-manipulators.html" class="sidebar-link">Stream manipulators</a></li><li><a href="/cpp/flow-control.html" class="sidebar-link">Flow Control</a></li><li><a href="/cpp/metaprogramming.html" class="sidebar-link">Metaprogramming</a></li><li><a href="/cpp/const-keyword.html" class="sidebar-link">const keyword</a></li><li><a href="/cpp/mutable-keyword.html" class="sidebar-link">mutable keyword</a></li><li><a href="/cpp/friend-keyword.html" class="sidebar-link">Friend keyword</a></li><li><a href="/cpp/type-keywords.html" class="sidebar-link">Type Keywords</a></li><li><a href="/cpp/basic-type-keywords.html" class="sidebar-link">Basic Type Keywords</a></li><li><a href="/cpp/variable-declaration-keywords.html" class="sidebar-link">Variable Declaration Keywords</a></li><li><a href="/cpp/keywords.html" class="sidebar-link">Keywords</a></li><li><a href="/cpp/returning-several-values-from-a-function.html" class="sidebar-link">Returning several values from a function</a></li><li><a href="/cpp/polymorphism.html" class="sidebar-link">Polymorphism</a></li><li><a href="/cpp/references.html" class="sidebar-link">References</a></li><li><a href="/cpp/value-and-reference-semantics.html" class="sidebar-link">Value and Reference Semantics</a></li><li><a href="/cpp/c-function-call-by-value-vs-call-by-reference.html" class="sidebar-link">C++ function "call by value" vs. "call by reference"</a></li><li><a href="/cpp/copying-vs-assignment.html" class="sidebar-link">Copying vs Assignment</a></li><li><a href="/cpp/pointers.html" class="sidebar-link">Pointers</a></li><li><a href="/cpp/pointers-to-members.html" class="sidebar-link">Pointers to members</a></li><li><a href="/cpp/the-this-pointer.html" class="sidebar-link">The This Pointer</a></li><li><a href="/cpp/smart-pointers.html" class="sidebar-link">Smart Pointers</a></li><li><a href="/cpp/classes-structures.html" class="sidebar-link">Classes/Structures</a></li><li><a href="/cpp/function-overloading.html" class="sidebar-link">Function Overloading</a></li><li><a href="/cpp/operator-overloading.html" class="sidebar-link">Operator Overloading</a></li><li><a href="/cpp/function-template-overloading.html" class="sidebar-link">Function Template Overloading</a></li><li><a href="/cpp/virtual-member-functions.html" class="sidebar-link">Virtual Member Functions</a></li><li><a href="/cpp/inline-functions.html" class="sidebar-link">Inline functions</a></li><li><a href="/cpp/special-member-functions.html" class="sidebar-link">Special Member Functions</a></li><li><a href="/cpp/non-static-member-functions.html" class="sidebar-link">Non-Static Member Functions</a></li><li><a href="/cpp/constant-class-member-functions.html" class="sidebar-link">Constant class member functions</a></li><li><a href="/cpp/c-containers.html" class="sidebar-link">C++ Containers</a></li><li><a href="/cpp/namespaces.html" class="sidebar-link">Namespaces</a></li><li><a href="/cpp/header-files.html" class="sidebar-link">Header Files</a></li><li><a href="/cpp/using-declaration.html" class="sidebar-link">Using declaration</a></li><li><a href="/cpp/std-string.html" class="sidebar-link">std::string</a></li><li><a href="/cpp/std-array.html" class="sidebar-link">std::array</a></li><li><a href="/cpp/std-vector.html" class="sidebar-link">std::vector</a></li><li><a href="/cpp/std-map.html" class="sidebar-link">std::map</a></li><li><a href="/cpp/std-optional.html" class="sidebar-link">std::optional</a></li><li><a href="/cpp/std-function-to-wrap-any-element-that-is-callable.html" class="sidebar-link">std::function: To wrap any element that is callable</a></li><li><a href="/cpp/std-forward-list.html" class="sidebar-link">std::forward_list</a></li><li><a href="/cpp/std-pair.html" class="sidebar-link">std::pair</a></li><li><a href="/cpp/std-atomics.html" class="sidebar-link">std::atomics</a></li><li><a href="/cpp/std-variant.html" class="sidebar-link">std::variant</a></li><li><a href="/cpp/std-iomanip.html" class="sidebar-link">std::iomanip</a></li><li><a href="/cpp/std-any.html" class="sidebar-link">std::any</a></li><li><a href="/cpp/std-set-and-std-multiset.html" class="sidebar-link">std::set and std::multiset</a></li><li><a href="/cpp/std-integer-sequence.html" class="sidebar-link">std::integer_sequence</a></li><li><a href="/cpp/using-std-unordered-map.html" class="sidebar-link">Using std::unordered_map</a></li><li><a href="/cpp/standard-library-algorithms.html" class="sidebar-link">Standard Library Algorithms</a></li><li><a href="/cpp/the-iso-c-standard.html" class="sidebar-link">The ISO C++ Standard</a></li><li><a href="/cpp/inline-variables.html" class="sidebar-link">Inline variables</a></li><li><a href="/cpp/random-number-generation.html" class="sidebar-link">Random number generation</a></li><li><a href="/cpp/date-and-time-using-chrono-header.html" class="sidebar-link">Date and time using header</a></li><li><a href="/cpp/sorting.html" class="sidebar-link">Sorting</a></li><li><a href="/cpp/enumeration.html" aria-current="page" class="active sidebar-link">Enumeration</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/cpp/enumeration.html#iteration-over-an-enum" class="sidebar-link">Iteration over an enum</a></li><li class="sidebar-sub-header"><a href="/cpp/enumeration.html#basic-enumeration-declaration" class="sidebar-link">Basic Enumeration Declaration</a></li><li class="sidebar-sub-header"><a href="/cpp/enumeration.html#enumeration-in-switch-statements" class="sidebar-link">Enumeration in switch statements</a></li><li class="sidebar-sub-header"><a href="/cpp/enumeration.html#scoped-enums" class="sidebar-link">Scoped enums</a></li><li class="sidebar-sub-header"><a href="/cpp/enumeration.html#enum-forward-declaration-in-c-11" class="sidebar-link">Enum forward declaration in C++11</a></li></ul></li><li><a href="/cpp/iteration.html" class="sidebar-link">Iteration</a></li><li><a href="/cpp/regular-expressions.html" class="sidebar-link">Regular expressions</a></li><li><a href="/cpp/implementation-defined-behavior.html" class="sidebar-link">Implementation-defined behavior</a></li><li><a href="/cpp/exceptions.html" class="sidebar-link">Exceptions</a></li><li><a href="/cpp/lambdas.html" class="sidebar-link">Lambdas</a></li><li><a href="/cpp/value-categories.html" class="sidebar-link">Value Categories</a></li><li><a href="/cpp/preprocessor.html" class="sidebar-link">Preprocessor</a></li><li><a href="/cpp/data-structures-in-c.html" class="sidebar-link">Data Structures in C++</a></li><li><a href="/cpp/templates.html" class="sidebar-link">Templates</a></li><li><a href="/cpp/expression-templates.html" class="sidebar-link">Expression templates</a></li><li><a href="/cpp/curiously-recurring-template-pattern-crtp.html" class="sidebar-link">Curiously Recurring Template Pattern (CRTP)</a></li><li><a href="/cpp/threading.html" class="sidebar-link">Threading</a></li><li><a href="/cpp/thread-synchronization-structures.html" class="sidebar-link">Thread synchronization structures</a></li><li><a href="/cpp/the-rule-of-three-five-and-zero.html" class="sidebar-link">The Rule of Three, Five, And Zero</a></li><li><a href="/cpp/raii-resource-acquisition-is-initialization.html" class="sidebar-link">RAII: Resource Acquisition Is Initialization</a></li><li><a href="/cpp/rtti-run-time-type-information.html" class="sidebar-link">RTTI: Run-Time Type Information</a></li><li><a href="/cpp/mutexes.html" class="sidebar-link">Mutexes</a></li><li><a href="/cpp/recursive-mutex.html" class="sidebar-link">Recursive Mutex</a></li><li><a href="/cpp/semaphore.html" class="sidebar-link">Semaphore</a></li><li><a href="/cpp/futures-and-promises.html" class="sidebar-link">Futures and Promises</a></li><li><a href="/cpp/atomic-types.html" class="sidebar-link">Atomic Types</a></li><li><a href="/cpp/type-erasure.html" class="sidebar-link">Type Erasure</a></li><li><a href="/cpp/explicit-type-conversions.html" class="sidebar-link">Explicit type conversions</a></li><li><a href="/cpp/unnamed-types.html" class="sidebar-link">Unnamed types</a></li><li><a href="/cpp/type-traits.html" class="sidebar-link">Type Traits</a></li><li><a href="/cpp/return-type-covariance.html" class="sidebar-link">Return Type Covariance</a></li><li><a href="/cpp/layout-of-object-types.html" class="sidebar-link">Layout of object types</a></li><li><a href="/cpp/type-inference.html" class="sidebar-link">Type Inference</a></li><li><a href="/cpp/typedef-and-type-aliases.html" class="sidebar-link">Typedef and type aliases</a></li><li><a href="/cpp/type-deduction.html" class="sidebar-link">type deduction</a></li><li><a href="/cpp/trailing-return-type.html" class="sidebar-link">Trailing return type</a></li><li><a href="/cpp/alignment.html" class="sidebar-link">Alignment</a></li><li><a href="/cpp/perfect-forwarding.html" class="sidebar-link">Perfect Forwarding</a></li><li><a href="/cpp/decltype.html" class="sidebar-link">decltype</a></li><li><a href="/cpp/sfinae-substitution-failure-is-not-an-error.html" class="sidebar-link">SFINAE (Substitution Failure Is Not An Error)</a></li><li><a href="/cpp/undefined-behavior.html" class="sidebar-link">Undefined Behavior</a></li><li><a href="/cpp/overload-resolution.html" class="sidebar-link">Overload resolution</a></li><li><a href="/cpp/move-semantics.html" class="sidebar-link">Move Semantics</a></li><li><a href="/cpp/pimpl-idiom.html" class="sidebar-link">Pimpl Idiom</a></li><li><a href="/cpp/auto.html" class="sidebar-link">auto</a></li><li><a href="/cpp/copy-elision.html" class="sidebar-link">Copy Elision</a></li><li><a href="/cpp/fold-expressions.html" class="sidebar-link">Fold Expressions</a></li><li><a href="/cpp/unions.html" class="sidebar-link">Unions</a></li><li><a href="/cpp/design-pattern-implementation-in-c.html" class="sidebar-link">Design pattern implementation in C++</a></li><li><a href="/cpp/singleton-design-pattern.html" class="sidebar-link">Singleton Design Pattern</a></li><li><a href="/cpp/user-defined-literals.html" class="sidebar-link">User-Defined Literals</a></li><li><a href="/cpp/memory-management.html" class="sidebar-link">Memory management</a></li><li><a href="/cpp/c-11-memory-model.html" class="sidebar-link">C++11 Memory Model</a></li><li><a href="/cpp/scopes.html" class="sidebar-link">Scopes</a></li><li><a href="/cpp/static-assert.html" class="sidebar-link">static_assert</a></li><li><a href="/cpp/constexpr.html" class="sidebar-link">constexpr</a></li><li><a href="/cpp/one-definition-rule-odr.html" class="sidebar-link">One Definition Rule (ODR)</a></li><li><a href="/cpp/unspecified-behavior.html" class="sidebar-link">Unspecified behavior</a></li><li><a href="/cpp/argument-dependent-name-lookup.html" class="sidebar-link">Argument Dependent Name Lookup</a></li><li><a href="/cpp/attributes.html" class="sidebar-link">Attributes</a></li><li><a href="/cpp/recursion-in-c.html" class="sidebar-link">Recursion in C++</a></li><li><a href="/cpp/arithmitic-metaprogramming.html" class="sidebar-link">Arithmitic Metaprogramming</a></li><li><a href="/cpp/callable-objects.html" class="sidebar-link">Callable Objects</a></li><li><a href="/cpp/client-server-examples.html" class="sidebar-link">Client server examples</a></li><li><a href="/cpp/const-correctness.html" class="sidebar-link">Const Correctness</a></li><li><a href="/cpp/parameter-packs.html" class="sidebar-link">Parameter packs</a></li><li><a href="/cpp/build-systems.html" class="sidebar-link">Build Systems</a></li><li><a href="/cpp/concurrency-with-openmp.html" class="sidebar-link">Concurrency With OpenMP</a></li><li><a href="/cpp/resource-management.html" class="sidebar-link">Resource Management</a></li><li><a href="/cpp/storage-class-specifiers.html" class="sidebar-link">Storage class specifiers</a></li><li><a href="/cpp/linkage-specifications.html" class="sidebar-link">Linkage specifications</a></li><li><a href="/cpp/digit-separators.html" class="sidebar-link">Digit separators</a></li><li><a href="/cpp/c-incompatibilities.html" class="sidebar-link">C incompatibilities</a></li><li><a href="/cpp/side-by-side-comparisons-of-classic-c-examples-solved-via-c-vs-c-11-vs-c-14-vs-c-17.html" class="sidebar-link">Side by Side Comparisons of classic C++ examples solved via C++ vs C++11 vs C++14 vs C++17</a></li><li><a href="/cpp/compiling-and-building.html" class="sidebar-link">Compiling and Building</a></li><li><a href="/cpp/common-compile-linker-errors-gcc.html" class="sidebar-link">Common compile/linker errors (GCC)</a></li><li><a href="/cpp/more-undefined-behaviors-in-c.html" class="sidebar-link">More undefined behaviors in C++</a></li><li><a href="/cpp/unit-testing-in-c.html" class="sidebar-link">Unit Testing in C++</a></li><li><a href="/cpp/c-debugging-and-debug-prevention-tools-techniques.html" class="sidebar-link">C++ Debugging and Debug-prevention Tools & Techniques</a></li><li><a href="/cpp/optimization-in-c.html" class="sidebar-link">Optimization in C++</a></li><li><a href="/cpp/optimization.html" class="sidebar-link">Optimization</a></li><li><a href="/cpp/profiling.html" class="sidebar-link">Profiling</a></li><li><a href="/cpp/refactoring-techniques.html" class="sidebar-link">Refactoring Techniques</a></li><li><a href="/cpp/internationalization-in-c.html" class="sidebar-link">Internationalization in C++</a></li><li><a href="/cpp/contributors.html" class="sidebar-link">The Contributors</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="enumeration"><a href="#enumeration" class="header-anchor">#</a> Enumeration</h1> <h2 id="iteration-over-an-enum"><a href="#iteration-over-an-enum" class="header-anchor">#</a> Iteration over an enum</h2> <p>There is no built-in to iterate over enumeration.</p> <p>But there are several ways</p> <li>
for `enum` with only consecutive values:
<div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">enum</span> <span class="token class-name">E</span> <span class="token punctuation">{</span>
Begin<span class="token punctuation">,</span>
E1 <span class="token operator">=</span> Begin<span class="token punctuation">,</span>
E2<span class="token punctuation">,</span>
<span class="token comment">// ..</span>
En<span class="token punctuation">,</span>
End
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span>E e <span class="token operator">=</span> E<span class="token double-colon punctuation">::</span>Begin<span class="token punctuation">;</span> e <span class="token operator">!=</span> E<span class="token double-colon punctuation">::</span>End<span class="token punctuation">;</span> <span class="token operator">++</span>e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// Do job with e</span>
<span class="token punctuation">}</span>
</code></pre></div></li> <p>with <code>enum class</code>, <code>operator ++</code> has to be implemented:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code>E<span class="token operator">&</span> <span class="token keyword">operator</span> <span class="token operator">++</span> <span class="token punctuation">(</span>E<span class="token operator">&</span> e<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>e <span class="token operator">==</span> E<span class="token double-colon punctuation">::</span>End<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">throw</span> std<span class="token double-colon punctuation">::</span><span class="token function">out_of_range</span><span class="token punctuation">(</span><span class="token string">"for E& operator ++ (E&)"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
e <span class="token operator">=</span> <span class="token function">E</span><span class="token punctuation">(</span><span class="token generic-function"><span class="token function">static_cast</span><span class="token generic class-name"><span class="token operator"><</span>std<span class="token double-colon punctuation">::</span>underlying_type<span class="token operator"><</span>E<span class="token operator">></span><span class="token double-colon punctuation">::</span>type<span class="token operator">></span></span></span><span class="token punctuation">(</span>e<span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> e<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><li>
using a container as `std::vector`
<div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">enum</span> <span class="token class-name">E</span> <span class="token punctuation">{</span>
E1 <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">,</span>
E2 <span class="token operator">=</span> <span class="token number">8</span><span class="token punctuation">,</span>
<span class="token comment">// ..</span>
En
<span class="token punctuation">}</span><span class="token punctuation">;</span>
std<span class="token double-colon punctuation">::</span>vector<span class="token operator"><</span>E<span class="token operator">></span> <span class="token function">build_all_E</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
<span class="token keyword">const</span> E all<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span>E1<span class="token punctuation">,</span> E2<span class="token punctuation">,</span> <span class="token comment">/*..*/</span> En<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> std<span class="token double-colon punctuation">::</span><span class="token generic-function"><span class="token function">vector</span><span class="token generic class-name"><span class="token operator"><</span>E<span class="token operator">></span></span></span><span class="token punctuation">(</span>all<span class="token punctuation">,</span> all <span class="token operator">+</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>all<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>E<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
std<span class="token double-colon punctuation">::</span>vector<span class="token operator"><</span>E<span class="token operator">></span> all_E <span class="token operator">=</span> <span class="token function">build_all_E</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>and then</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">for</span> <span class="token punctuation">(</span>std<span class="token double-colon punctuation">::</span>vector<span class="token operator"><</span>E<span class="token operator">></span><span class="token double-colon punctuation">::</span>const_iterator it <span class="token operator">=</span> all_E<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it <span class="token operator">!=</span> all_E<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">++</span>it<span class="token punctuation">)</span> <span class="token punctuation">{</span>
E e <span class="token operator">=</span> <span class="token operator">*</span>it<span class="token punctuation">;</span>
<span class="token comment">// Do job with e;</span>
<span class="token punctuation">}</span>
</code></pre></div></li> <li>
or `std::initializer_list` and a simpler syntax:
<div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">enum</span> <span class="token class-name">E</span> <span class="token punctuation">{</span>
E1 <span class="token operator">=</span> <span class="token number">4</span><span class="token punctuation">,</span>
E2 <span class="token operator">=</span> <span class="token number">8</span><span class="token punctuation">,</span>
<span class="token comment">// ..</span>
En
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">constexpr</span> std<span class="token double-colon punctuation">::</span>initializer_list<span class="token operator"><</span>E<span class="token operator">></span> all_E <span class="token operator">=</span> <span class="token punctuation">{</span>E1<span class="token punctuation">,</span> E2<span class="token punctuation">,</span> <span class="token comment">/*..*/</span> En<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><p>and then</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">auto</span> e <span class="token operator">:</span> all_E<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// Do job with e</span>
<span class="token punctuation">}</span>
</code></pre></div></li> <h2 id="basic-enumeration-declaration"><a href="#basic-enumeration-declaration" class="header-anchor">#</a> Basic Enumeration Declaration</h2> <p>Standard enumerations allow users to declare a useful name for a set of integers. The names are collectively referred to as enumerators. An enumeration and its associated enumerators are defined as follows:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">enum</span> <span class="token class-name">myEnum</span>
<span class="token punctuation">{</span>
enumName1<span class="token punctuation">,</span>
enumName2<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><p>An enumeration is a <strong>type</strong>, one which is distinct from all other types. In this case, the name of this type is <code>myEnum</code>. Objects of this type are expected to assume the value of an enumerator within the enumeration.</p> <p>The enumerators declared within the enumeration are constant values of the type of the enumeration. Though the enumerators are declared within the type, the scope operator <code>::</code> is not needed to access the name. So the name of the first enumerator is <code>enumName1</code>.</p> <p>The scope operator can be optionally used to access an enumerator within an enumeration. So <code>enumName1</code> can also be spelled <code>myEnum::enumName1</code>.</p> <p>Enumerators are assigned integer values starting from 0 and increasing by 1 for each enumerator in an enumeration. So in the above case, <code>enumName1</code> has the value 0, while <code>enumName2</code> has the value 1.</p> <p>Enumerators can also be assigned a specific value by the user; this value must be an integral constant expression. Enumerators who's values are not explicitly provided will have their value set to the value of the previous enumerator + 1.</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">enum</span> <span class="token class-name">myEnum</span>
<span class="token punctuation">{</span>
enumName1 <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token comment">// value will be 1</span>
enumName2 <span class="token operator">=</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token comment">// value will be 2</span>
enumName3<span class="token punctuation">,</span> <span class="token comment">// value will be 3, previous value + 1</span>
enumName4 <span class="token operator">=</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token comment">// value will be 7</span>
enumName5<span class="token punctuation">,</span> <span class="token comment">// value will be 8</span>
enumName6 <span class="token operator">=</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token comment">// value will be 5, legal to go backwards</span>
enumName7 <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token comment">// value will be 3, legal to reuse numbers</span>
enumName8 <span class="token operator">=</span> enumName4 <span class="token operator">+</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token comment">// value will be 9, legal to take prior enums and adjust them</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h2 id="enumeration-in-switch-statements"><a href="#enumeration-in-switch-statements" class="header-anchor">#</a> Enumeration in switch statements</h2> <p>A common use for enumerators is for switch statements and so they commonly appear in state machines. In fact a useful feature of switch statements with enumerations is that if no default statement is included for the switch, and not all values of the enum have been utilized, the compiler will issue a warning.</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">enum</span> <span class="token class-name">State</span> <span class="token punctuation">{</span>
start<span class="token punctuation">,</span>
middle<span class="token punctuation">,</span>
end
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">switch</span><span class="token punctuation">(</span>myState<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">case</span> start<span class="token operator">:</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">case</span> middle<span class="token operator">:</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span> <span class="token comment">// warning: enumeration value 'end' not handled in switch [-Wswitch]</span>
</code></pre></div><h2 id="scoped-enums"><a href="#scoped-enums" class="header-anchor">#</a> Scoped enums</h2> <p>C++11 introduces what are known as <strong>scoped enums</strong>. These are enumerations whose members must be qualified with <code>enumname::membername</code>. Scoped enums are declared using the <code>enum class</code> syntax. For example, to store the colors in a rainbow:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">enum</span> <span class="token keyword">class</span> <span class="token class-name">rainbow</span> <span class="token punctuation">{</span>
RED<span class="token punctuation">,</span>
ORANGE<span class="token punctuation">,</span>
YELLOW<span class="token punctuation">,</span>
GREEN<span class="token punctuation">,</span>
BLUE<span class="token punctuation">,</span>
INDIGO<span class="token punctuation">,</span>
VIOLET
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><p>To access a specific color:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code>rainbow r <span class="token operator">=</span> rainbow<span class="token double-colon punctuation">::</span>INDIGO<span class="token punctuation">;</span>
</code></pre></div><p><code>enum class</code>es cannot be implicitly converted to <code>int</code>s without a cast. So <code>int x = rainbow::RED</code> is invalid.</p> <p>Scoped enums also allow you to specify the <strong>underlying type</strong>, which is the type used to represent a member. By default it is <code>int</code>. In a Tic-Tac-Toe game, you may store the piece as</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token keyword">enum</span> <span class="token keyword">class</span> <span class="token class-name">piece</span> <span class="token operator">:</span> <span class="token base-clause"><span class="token keyword">char</span></span> <span class="token punctuation">{</span>
EMPTY <span class="token operator">=</span> <span class="token string">'\0'</span><span class="token punctuation">,</span>
X <span class="token operator">=</span> <span class="token string">'X'</span><span class="token punctuation">,</span>
O <span class="token operator">=</span> <span class="token string">'O'</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><p>As you may notice, <code>enum</code>s can have a trailing comma after the last member.</p> <h2 id="enum-forward-declaration-in-c-11"><a href="#enum-forward-declaration-in-c-11" class="header-anchor">#</a> Enum forward declaration in C++11</h2> <p>Scoped enumerations:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">enum</span> <span class="token keyword">class</span> <span class="token class-name">Status</span><span class="token punctuation">;</span> <span class="token comment">// Forward declaration </span>
Status <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Use the forward declaration</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">enum</span> <span class="token keyword">class</span> <span class="token class-name">Status</span> <span class="token punctuation">{</span> Invalid<span class="token punctuation">,</span> Success<span class="token punctuation">,</span> Fail <span class="token punctuation">}</span><span class="token punctuation">;</span>
Status <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// Full declaration required for implementation</span>
<span class="token punctuation">{</span>
<span class="token keyword">return</span> Status<span class="token double-colon punctuation">::</span>Success<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>Unscoped enumerations:</p> <div class="language-cpp extra-class"><pre class="language-cpp"><code><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">enum</span> <span class="token class-name">Status</span><span class="token operator">:</span> <span class="token keyword">int</span><span class="token punctuation">;</span> <span class="token comment">// Forward declaration, explicit type required</span>
Status <span class="token function">doWork</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Use the forward declaration</span>
<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token keyword">enum</span> <span class="token class-name">Status</span><span class="token operator">:</span> <span class="token keyword">int</span><span class="token punctuation">{</span> Invalid<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> Success<span class="token punctuation">,</span> Fail <span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">// Must match forward declare type</span>
<span class="token keyword">static_assert</span><span class="token punctuation">(</span> Success <span class="token operator">==</span> <span class="token number">1</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>An in-depth multi-file example can be found here: <a href="http://stackoverflow.com/a/19074269/1873507" target="_blank" rel="noopener noreferrer">Blind fruit merchant example<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/devtut/generate/edit/master/docs/cpp/enumeration.md" target="_blank" rel="noopener noreferrer">Edit this page on GitHub</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
←
<a href="/cpp/sorting.html" class="prev">
Sorting
</a></span> <span class="next"><a href="/cpp/iteration.html">
Iteration
</a>
→
</span></p></div> </main></div><div class="global-ui"><!----></div></div>
<script src="/assets/js/app.1779e102.js" defer></script><script src="/assets/js/3.2cfa8016.js" defer></script><script src="/assets/js/648.b9166d4d.js" defer></script>
</body>
</html>