user3063807
user3063807

Reputation: 41

nonspecific enum function parameter

Im trying to write a function that takes an enum as a parameter, just as a function would take an int or bool etc.

I am using c++ so for example void Func (enum Blah, int blah);

any idea how I can let the function take a nonspecific enum as an argument?

I know I could just cast enums as ints and pass them through like this

enum foo{BAR};

void Func (int myenum, int blah); Func ((int)BAR, 0) However I don't want to be able to pass through anything but an enum.

I have heard of generics, I'm not sure if they are the answer because I couldn't find any definitive information on them.

So either some code or a link that would help me would be much appreciated. Thanks in advance.

Upvotes: 0

Views: 118

Answers (1)

John Zwinck
John Zwinck

Reputation: 249502

template <typename Enum>
void Func (Enum blah, int fleh)
{
  static_assert(std::is_enum<Enum>::value, "Only enums are allowed here");
}

Upvotes: 0

Related Questions