19

From questions such as this and this, I was under the impression that inheriting from a primitive type would cause a compiler error. However, the following code compiles and produces expected output on Ideone.

#include <iostream>

enum class Test : unsigned short int
{
    TEST, TEST2, TEST3, TEST4
};

int main() {
    // your code goes here
    Test ans = Test::TEST3;

    if(ans == Test::TEST3)
    {
        std::cout << "Here" << std::endl;
    }

    return 0;
}

Does the fact that the class is also an enum change the answers in the first two Q&A's? Is this well-defined behavior by the standard?

Community
  • 1
  • 1
R_Kapp
  • 2,818
  • 1
  • 18
  • 32
  • 2
    The referenced questions are about *classes*, not *enum classes*. Enum classes *can* ‘inherit’ from arithmetic types (I *think* it even defaults to `int`), but *can't* inherit from classes. – Biffen Feb 09 '16 at 14:31
  • 13
    This is an example of the "extend the language by overloading existing syntax to mean something different because we won't add new keywords" strategy. – molbdnilo Feb 09 '16 at 14:44

3 Answers3

33

This does not mean inheritance, this selects the enum's underlying type. The underlying type is the integral type which is used to represent the enumerator values.

You can see the difference in this example:

#include <iostream>

enum class tiny : bool {
    one, two   
};

enum class bigger : long {
    some, lots   
};

int main(int argc, char *argv[])
{
    std::cout << sizeof(tiny::one) << '\n';    //prints 1
    std::cout << sizeof(bigger::some) << '\n'; //prints 8
}

In C++11 you can specify the underlying type of both scoped (i.e. class) and unscoped enums.

TartanLlama
  • 63,752
  • 13
  • 157
  • 193
  • 2
    As an additional comment -- selecting the underlying type is also what allows you to forward declare enum classes, one of the new things that you can't do with C++03 enums. It's definitely supported. – Chris Beck Feb 10 '16 at 06:08
11

enum class was added in C++11 to allow you to specify the underlying type for an enumeration. It reuses the syntax for inheritance as an analogy, but it is not inheritance.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • 4
    "*`enum class` was added in C++11 to allow you to specify the underlying type for an enumeration.*" No, it was not; C++11 allowed regular `enum`s` to do that too. The only things `enum class` changes from a regular `enum` are: 1) require scoping of the enumerators by the EC's name. 2) forbid implicit conversions to the underlying class type. – Nicol Bolas Feb 10 '16 at 00:02
  • @NicolBolas - you say "no, it was not", then go on to explain that that's exactly what it does; the fact that there is **another** way to do it doesn't change that. – Pete Becker Feb 10 '16 at 01:28
  • 3
    Your statement says that `enum class` "was added [...] to allow you...". That implies that this was the sole or at least primary justification for it. When in fact it has nothing to do with `enum class` *specifically*. – Nicol Bolas Feb 10 '16 at 01:52
6

It is not inheritance, it is the way to specify underlying type.

Jarod42
  • 203,559
  • 14
  • 181
  • 302