Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: enum type as template argument - global scope

I've a this situation:

template<typename myEnumType>
int foo(const myEnumType & shortest_paths_algorithm)
{
    ...
}

int main()
{
    myEnumType enum_type_istance;
    int a = foo(enum_type_istance)
}

if I declare

typedef enum {AAA, BBB} myEnumType;

before the function declaration everything is ok. While, if I write the above line before creating enum_type_istance variable, get the error

no matching function for call to ‘foo(main()::myEnumType&)’ candidate is: template int foo(const myEnumType&)

why??? how can I type-define inside the main? thank you!

like image 962
user1403546 Avatar asked Feb 11 '26 19:02

user1403546


1 Answers

You are using C++ prior to C++11, which does not allow "local" types to be used in template arguments. This feature has been, luckily, introduced in C++11. As you can see it compiles just fine with the -std=c++11 flag, while it fails without.

like image 99
Shoe Avatar answered Feb 13 '26 08:02

Shoe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!