C :枚举类型作为模板参数 - 全局范围

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

本文关键字:参数 全局 范围 枚举 类型      更新时间:2023-10-16

我已经有这种情况:

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变量之前写上述行,请获取错误

无匹配函数呼叫" foo(main():: myenumtype&amp;)" 候选人是:template int foo(const myenumtype&amp;)

为什么???如何在主内输入定义?谢谢!

您在C 11之前使用的是C ,这不允许在模板参数中使用"本地"类型。幸运的是,此功能是在C 11中引入的。如您所见,它可以使用-std=c++11标志来编译它,而它会在没有的情况下进行。