Finite enumeration classes in C ++ 11

I'm just wondering if the enum class can be final or not ... since compilers give me conflicting results.

Consider the code:

#include <iostream> enum class some_enums final : char { a = 'a', b = 'b', c = 'c' }; int main() { some_enums aa = some_enums::a; std::cout << "aa=" << static_cast<char>(aa) << std::endl; } 

Compiling with the Visual Studio 2015 compiler ( http://rextester.com/l/cpp_online_compiler_visual ) works ... however, compiling with clang ( http://rextester.com/l/cpp_online_compiler_clang ) gives me an error:

 source_file.cpp:3:30: error: expected ';' after top level declarator enum class some_enums final : char 

I did not see any traces of the final enumeration classes anywhere in the standard, so I give credit to clang ... however, why Visual Studio accepts it in this case, although it is not mentioned in MSDN ( https://msdn.microsoft.com/en- us / library / 2dzy4k6e.aspx )?

+7
c ++ enums c ++ 11 final
source share
1 answer

The final specifier is used to indicate that the class cannot inherit. Since the enum class cannot be inherited, the final specifier used in your case is useless.

The "Stollen" from here , as well as the mention in ยง7.2 / p1 of the Listing Declaration [dcl.enum], the class enum declaration is of the form:

 enum-key attr(optional) nested-name-specifier(optional) identifier enum-base(optional) ; 
  • enum-key - one of enum , enum class (since C ++ 11) or enum struct (since C ++ 11)
  • attr (C ++ 11) - an optional sequence of any number of attributes
  • identifier - the name of the declared enumeration. If present, and if this declaration is a repeated declaration, it may be preceded by a nested-name-specifier (starting with C ++ 11): a sequence of names and operators with the resolution of the scope :: ending with the operator of the resolution of the scope. The name can only be omitted in the listing declaration without access.
  • enum-base (C ++ 11) - a colon (:), followed by type-specifier-seq , which names the integral type (if it is cv-qualified , qualifications are ignored).
  • enumerator-list - a list of definitions of enumerations, separated by commas, each of which is either just an identifier that becomes the name of the enumerator, or an identifier with an initializer: identifier = constexpr . In either case, the identifier may follow the sequence of the optional attribute attribute. (starting with C ++ 17).

Therefore, the definition of an enum class with a final qualifier is:

 enum class some_enums final : char { ... }; 

not a standard form.

+7
source share

All Articles