Reputation: 1830
I want to do this:
template <enum Type>
class Message {
private:
Type m_type
};
enum StdInMessages { PrintHello, Echo, ... };
class StdInMsg : Message<StdInMessages>
{ ... }
enum NetworkMessages { DoSomethingElse, Shutdown ... };
class NetworkMsg : Message<NetworkMessages>
{ ... }
Of course, the actual messages are slightly different
Why doesn't this work?
template <enum T> class ATemplate {};
I get this error
error: use of enum ‘T’ without previous declaration
Upvotes: 0
Views: 208
Reputation: 258698
Because that's not valid syntax for a template unless what you're looking for is what Konrad answered.
You need to either use typename
or class
.
The following should do it:
enum X
{
a
};
template <typename T> class ATemplate {};
ATemplate<X> A;
Upvotes: 1
Reputation: 546213
It works if enum T
is declared beforehand:
enum T {
foo, bar
};
template <enum T> // or simply `template <T>`
class ATemplate { };
int main() {
ATemplate<foo> x;
}
But judging from the variable name T
, this isn’t what you want. Since it’s hard to guess what exactly you want, you need to be more specific.
Upvotes: 2