Why can't an enumeration be a template?

enumeration cannot be a templateis an error that occurs when trying to compile with BCC64 (based on Clang) the following code:

template <typename T> enum class fooEnum : T
{
    a,b,c,d,e
};

At first I thought that this explicit prohibition was due to type restrictions enum, if the enum template type can be templated, then this can lead to malformed enums, but when we try this:

template <typename A> struct fooClass
{
    enum class fooEnum : A
    {
        a,b,c,d,e
    };
};

It compiles without problems if the type Afollows the same restrictions as the enumerated types listed below, an expression that defines the value of the enumeration:

  • Must be an integer constant large enough to match all enumeration values
  • Each type listed must be compatible with the integer type charor signed/ unsigned.

( ), , :

enum class fooEnum : fooClass
{
    a,b,c,d,e
};

'fooClass'

, , , . ?

.

+4
2

[++ standard 14.1], ,

.

, .

+1

, enum ( ).

#include <iostream>
using namespace std;

template <typename T>
struct myEnum
{
    enum Values
    {
        a=0, b=1,c=2
    };
};

int main() {
    myEnum<int> abc;
    cout<<abc.Values::a<< abc.Values::b<<abc.Values::c;
    // your code goes here
    return 0;
}

: 012

http://ideone.com/

+1

Source: https://habr.com/ru/post/1527956/


All Articles