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

标签 c++ function templates enums global

我有这种情况:

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/

相关文章:

c++ 模板和析构函数问题

c++ - 在 c++ sqlite3 中编码多对多关系

c++ - 从命令行构建 Visual Studio Express 11 项目

Javascript if/else 语句未按预期工作

c - 在 C 中的函数内部声明函数

c++ - 无法捕获模板特化方法抛出的异常

c++ - 为什么我的模板特化不起作用?

c++ - 将类作为函数参数传递

c++ - 通过 IShellFolder 接口(interface)枚举打印机文件夹导致堆分配泄漏

postgresql - 使用变量作为语句的一部分