pseudorandomusername
pseudorandomusername

Reputation: 57

Instantiate template using enum

Why does the following compilation fail with error: ‘arg’ cannot appear in a constant-expression?

class Foo {
public:
    enum myenum { BIRDY, NUMNUM };
    typedef enum myenum myenum_t;
    void bar(const myenum_t arg);
}
template<Foo::myenum_t> class MyClass {};
void Foo::bar(const myenum_t arg) {
    MyClass<arg> hey;
}

Are enumerated types not compile-time constants?

Upvotes: 1

Views: 216

Answers (2)

tonso
tonso

Reputation: 1768

Enum values are compile-time constants, but you are passing a variable of enum type, the value of which isn't determined until the program is executed.

Upvotes: 2

ForEveR
ForEveR

Reputation: 55887

You are trying to use variable, not just constant, there is no variables on compile-time, use template function.

template<myenum_t>
void bar();

template<Foo::myenum_t arg>
void Foo::bar()
{
   MyClass<arg> hey;
}

Upvotes: 4

Related Questions