c++c++11enumsfinal

Final enum classes in C++11


I am just curious whether an enum class can be final or not ... since the compilers are giving me contradictory 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 this with Visual Studio 2015 compiler (http://rextester.com/l/cpp_online_compiler_visual) works... however compiling it 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 have seen no traces of final enum classes anywhere in the standard so I give credit to clang ... however why does Visual Studio accept it in this case although it is not mentioned in MSDN (https://msdn.microsoft.com/en-us/library/2dzy4k6e.aspx) ?


Solution

  • The final specifier is used to signify that a class cannot be inherit from. Since an enum class cannot be inherited, the final specifier used in your case is useless.

    "Stollen" from here and also mention in §7.2/p1 Enumeration declarations [dcl.enum] , a class enum declaration is of the form:

    enum-key attr(optional) nested-name-specifier(optional) identifier enum-base(optional) ;
    

    Consequently, defining an enum class with final specifier as:

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

    is not a standard form.