Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>enum class</code> is called a <em>scoped enumeration</em>. It prevents polluting the namespace where the enumeration appears with the names of the enumerators.</p> <p>In C++03, you could do effectively the same thing by putting the <code>enum</code> inside a dedicated <code>class</code>. Perhaps that's the source of the syntax, which is a bit confusing.</p> <p>Another difference is that the enumerators of such a type don't convert implicitly to <code>int</code> (<code>static_cast&lt;int&gt;</code> is required). This may be seldom <em>needed</em> but it makes it safe to overload a function taking an <code>int</code> argument with one taking <code>enum</code> type. You can be sure the <code>int</code> won't be called by accident. Or you can define pseudo-integral types with dedicated <code>operator</code> functions, and be sure that built-in operators won't interfere.</p> <p>It's a bit annoying that these two unrelated differences come in the same package, and that you can't get an <em>unscoped</em> enumeration with no implicit conversion, but generally both changes are Good Things and <code>enum class</code> is a good default practice in C++11.</p> <p><strong>EDIT:</strong> A scoped enumeration is defined like this:</p> <pre><code>enum class duck { huey, dewey, louie }; </code></pre> <p>and must be used with the scope resolution operator <code>::</code> like this:</p> <pre><code>duck culprit = duck::huey; // or "auto culprit" to avoid redundancy </code></pre> <p>Note that the <code>::</code> operator also works with C++03 unscoped enumerations, so the second line above would work even if the first was missing <code>class</code>.</p> <p>This might be excessive detail, but <code>class</code> does not go into the <em>elaborated-type-specifier</em> if forward declaring the enumerated type, as in</p> <pre><code>void quack( enum duck whom ); // not "enum class" </code></pre> <p>However, there is a construct new in C++11, the <em>opaque-enum-declaration</em>, which does include the <code>class</code> keyword and defines a complete type.</p> <pre><code>enum duck; // duck is declared as incomplete type enum class duck; // duck is now complete type; underlying type defaults to int </code></pre> <p>The keyword <code>struct</code> can be substituted for <code>class</code> with no semantic difference.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload