1

我正在阅读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
4

1 回答 1

7

VC6 是一个古老的编译器,它对模板的支持严重不足,因此在许多情况下它无法处理合法代码。您应该放弃它并改为下载VS 2010 Express 。

于 2012-03-05T09:45:38.423 回答