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

时间:2012-03-05 09:41:58

标签: 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

1 个答案:

答案 0 :(得分:7)

VC6是一个古老的编译器,它对模板的支持非常缺乏,因此在许多情况下无法处理合法代码。您应该抛弃它并下载VS 2010 Express