c++ - 关于函数模板的相同代码块在 g++ 下编译正常,但在 VC6 下编译错误,为什么?

标签 c++ function-templates

我正在阅读C++ Primer 3rd edition的“函数模板”一章,当我尝试按照示例进行操作时,发现代码与本书几乎相同,但在编译过程中遇到错误在 VC6 下,但在 g++ 下一切正常。不知道为什么?

代码如下:

#include <iostream>
using namespace std;

template<typename T1, typename T2, typename T3>
T1 my_min(T2 a, T3 b)
{
    return a>b?b:a;
}

int main()
{
    int (*fp)(int, int) = &my_min<int>;
    cout<<fp(3,5)<<endl;
    return 0;
}

VC6下出现的错误如下:

error C2440: 'initializing' : cannot convert from '' to 'int (__cdecl *)(int,int)'
None of the functions with this name in scope match the target type

最佳答案

VC6 是一个古老的编译器,它对模板的支持严重缺乏,因此在很多情况下它无法应对合法的代码。你应该放弃它并下载VS 2010 Express相反。

关于c++ - 关于函数模板的相同代码块在 g++ 下编译正常,但在 VC6 下编译错误,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9564457/

相关文章:

c++ - 如何读取大 json?

数组大小为零时的 C++ 内存分配行为

c++ - 类成员函数模板可以是虚拟的吗?

c++如何将几个重载函数之一指定为模板函数中的参数

C++:函数模板问题

c++ - 如何在模板中返回正确的数据类型?

c++ - 如何通过扩展以下类型特征来删除 decltype(& MyClass::func) 部分?

c++ - libstdc++ 的 make_shared 布局是否在 gcc 4.x 和 gcc 6.x 之间发生了变化?

c++ - 用 SWIG 包装 std::complex<float> 的 4 维 std::vector

c++ - 传递函数模板的地址