关于函数模板的相同代码块在g++下编译正常,但在VC6下编译错误,原因是什么

Same code blocks about function template compiled ok under g++ but error under VC6, why?

本文关键字:编译 但在 VC6 错误 是什么 函数模板 代码 g++      更新时间:2023-10-16

我正在阅读C++Primer第三版的"函数模板"一章,当我试图遵循这个例子时,我发现代码几乎与本书相同。在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。

相关文章: