Your use of Class
for an enum
is a horrible idea. Don't use language keywords with capitalization differences as type names.
C
is a compile-time value of type Class
. It is not a type.
typename Cls = C
attempts to assign a value of type Class
to a type. This is an error akin to saying "picking up a sad". sad is not a noun, it is not something you can pick up.
The easiest way to make your code compile is to delete onlyLegalForSecondaryEntities
entirely, and all references to it.
In general, under the standard you cannot have a template method which is only valid when certain arguments are passed to the template class it exists within. Doing so makes your program ill formed, no diagnostic required.
This is close:
template<Class Cls = C,
std::enable_if_t< Cls == Secondary, int> =0
>
void onlyLegalForSecondaryEntities() {
std::cout << "Works" << std::endl;
}
except that even on Entity<Primary>
, you can do .onlyLegalForSecondaryEntities<Secondary>()
.
If you don't want to permit this, I'd use CRTP.
template<bool b, class D>
struct empty_if_false {};
template<class D>
struct empty_if_false<true, D> {
D* self() { return static_cast<D*>(this); }
D const* self() const { return static_cast<D*>(this); }
void onlyLegalForSecondaryEntities() {
// use self() instead of this in this method to get at a this pointer
std::cout << "Works" << std::endl;
}
};
then:
template<Class C>
class Entity:public empty_if_false< C==Secondary, Entity<C> > {
to conditionally have the method.