我有这种情况:
template<typename myEnumType>
int foo(const myEnumType & shortest_paths_algorithm)
{
...
}
int main()
{
myEnumType enum_type_istance;
int a = foo(enum_type_istance)
}
如果我声明
typedef enum {AAA, BBB} myEnumType;
在函数声明之前一切正常。而如果我在创建 enum_type_istance 变量之前写上面的行,就会得到错误
no matching function for call to ‘foo(main()::myEnumType&)’ candidate is: template int foo(const myEnumType&)
为什么???如何在 main 中进行类型定义? 谢谢!
最佳答案
您使用的是 C++11 之前的 C++,它不允许在模板参数中使用“本地”类型。幸运的是,此功能已在 C++11 中引入。作为you can see它使用 -std=c++11
标志编译得很好,而 it fails没有。
关于C++:枚举类型作为模板参数 - 全局范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21632042/