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

上面的代码在 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>          ]

如您所见,__stdcall修饰符的问题。这是编译器错误吗?

4

3 回答 3

10

不,这是设计使然。调用约定是函数声明的很大一部分,您的模板函数使用默认调用约定。除非您使用 /Gz 编译,否则这不是 __stdcall。默认为 /Gd,__cdecl。

当您以 x64 为目标时,代码会编译,因为它只有一个调用约定。

使固定:

template<class R, class T0, class T1>
class TFunction<R (__stdcall *)(T0,T1)>
{
    // etc..
};
于 2011-05-13T12:16:35.083 回答
4

这是因为 (*) 表示默认调用约定,即__cdecl.

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

实际上等于

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

当然,它不会匹配R(__stdcall *)(T0, T1)Win32 上__stdcall未被忽略的位置。如果您想部分专门化函数指针,那么您将需要为您想要接受的每个调用约定提供部分规范。

于 2011-05-13T12:16:11.437 回答
1

您还没有为 stdcall 案例专门化您的模板,即您需要

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

不确定语法,未经测试,但这应该是问题所在。

于 2011-05-13T12:16:57.890 回答