0
enum MyEnum{
  task1 = 0,
  task2 
};

// template<MyEnum T>  works
template<class T>
void fun(){

}


int main(){
 fun<MyEnum::task1>();
//  fun<int>(); works
}

How to create template type of enum

When I try to create a template out of enum I get error saying no matching function for call to ‘fun()

Why does the int work by not enum type?

When I do template<MyEnum T> it works but I dont understand why.

4
  • 4
    MyEnum::task1 is a value. MyEnum is a type Commented Feb 21, 2019 at 22:52
  • Why does doing template<MyEnum T> works then? Commented Feb 21, 2019 at 22:54
  • Because then you aren't asking for a type, you're asking for a value. If you have typename or class you are asking for a type. If you have a type, then you are asking for a value. Commented Feb 21, 2019 at 22:57
  • 3
    @mato template<MyEnum T> is a non-type template parameter. Similar to template<int I>. Commented Feb 21, 2019 at 22:57

1 Answer 1

1

Why does the int work by not enum type?

It does work for enum type just like it does for int type.

Just like fun<int>(); works, fun<MyEnum>(); works


And just like fun<1>(); doesn't work, fun<MyEnum::task1>(); doesn't work.


To make it work for values, like 1 or MyEnum::task1, you need to declare the template parameter differently, e.g.

template<int v>

or

template<MyEnum v>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.