Reputation: 24705
Could anyone tell me why this doesn't work?
enum CompCriteria{ByKey,ByValue,ByeKeyAndValue};
template<class T>
struct X;
template<>
struct X<CompCriteria>
{
};
int _tmain(int argc, _TCHAR* argv[])
{
X<CompCriteria::ByeKeyAndValue> x;
return 0;
}
Upvotes: 5
Views: 3973
Reputation: 53339
You're conflating the idea of parameterized types and parameterized values. A template parameter can be a type, or a constant. For example:
template <typename T>
struct Foo;
versus..
template <int N>
struct Foo;
It looks like you want to specialize your template based on an enum constant, rather than a type. Meaning, you need to say:
enum CompCriteria{ByKey,ByValue,ByeKeyAndValue};
template<CompCriteria>
struct X;
// Specialization for ByKeyAndValue
//
template<>
struct X<ByeKeyAndValue>
{
};
int main()
{
X<ByeKeyAndValue> x; // instantiate specialization
return 0;
}
Also, you can't refer to enums using the namespace
operator. If you want to encapsulate your enums, you need to wrap them in a namespace:
namespace CompCriteria
{
enum type {ByKey,ByValue,ByeKeyAndValue};
}
Upvotes: 9
Reputation: 99725
You have specialized X
for a type, but you're trying to use it with the integer CompCriteria::ByeKeyAndValue
.
You can specialize template class for the enum CompCriteria
underlying type - int
in this case, as follows:
enum CompCriteria{ByKey,ByValue,ByeKeyAndValue};
template<int>
struct X;
template<>
struct X<ByeKeyAndValue>
{
};
int main()
{
X<ByeKeyAndValue> x;
return 0;
}
Upvotes: 2
Reputation: 27023
enum CompCriteria{ByKey,ByValue,ByeKeyAndValue};
template<CompCriteria crit_>
struct X
{
static const CompCriteria crit = crit_;
};
int _tmain(int argc, _TCHAR* argv[])
{
X<CompCriteria::ByeKeyAndValue> x;
return 0;
}
Upvotes: 2
Reputation: 72329
If you have a template<class T>
= template<typename T>
, then T is expected to be, well, a type.
enum CompCriteria
is a type, so you can instantiate that template with it. A single value of the enum, however, is not a type, so you can't.
Upvotes: 2