Search code examples
c++function-templates

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


I am reading the chapter "function templates" of C++ Primer 3rd edition, and when I tried to follow the example, I found the code almost the same as the book has encountered an error during compilation under VC6 but everything is ok under g++. I don't know why?

Here is the code:

#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;
}

The error happened under VC6 appears like:

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

Solution

  • VC6 is an ancient compiler whose support for templates is woefully lacking, so it cannot cope with legal code in many circumstances. You should ditch it and download VS 2010 Express instead.