__stdcall函数指针的模板部分特化

pur*_*ess 8 c++ templates stdcall template-specialization

typedef bool (*my_function_f)(int, double);
typedef bool (__stdcall *my_function_f2)(int, double);
//            ^^^^^^^^^

template<class F> class TFunction;

template<class R, class T0, class T1>
class TFunction<R(*)(T0,T1)>
{
  typedef R (*func_type)(T0,T1);
};

int main()
{
  TFunction<my_function_f> t1;  // works on x64 and win32
  TFunction<my_function_f2> t2; // works on x64 and doesn't work on win32

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

上面的代码在Visual C++ 2010中给出了以下错误:

1>e:\project\orwell\head\multimapwizard\trunk\externals.cpp(49): error C2079: 't2' uses undefined class 'Externals::TFunction<F>'
1>          with
1>          [
1>              F=Externals::my_function_f2
1>          ]
Run Code Online (Sandbox Code Playgroud)

正如您可以看到__stdcall修饰符的问题.这是编译器错误吗?

Han*_*ant 10

不,这是设计的.调用约定是函数声明的一部分,您的模板函数使用默认的调用约定.除非用/ Gz编译,否则不是__stdcall.默认值为/ Gd,__ cdecl.

当您定位x64时,代码会编译,因为它幸福地只有一个调用约定.

固定:

template<class R, class T0, class T1>
class TFunction<R (__stdcall *)(T0,T1)>
{
    // etc..
};
Run Code Online (Sandbox Code Playgroud)