__stdcall 函数指针的模板部分特化 [英] Template partial specialization for __stdcall function pointer

查看:49
本文介绍了__stdcall 函数指针的模板部分特化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 中给了我以下错误:

The code above gives me the following error in 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 修饰符存在问题.这是编译器的错误吗?

As you can see the problem with __stdcall modifier. Is this the compiler bug?

推荐答案

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

No, this is by design. The calling convention is very much part of the function declaration, your template function uses the default calling convention. Which is not __stdcall unless you compile with /Gz. The default is /Gd, __cdecl.

当您面向 x64 时代码会编译,因为它只有一种调用约定.

The code compiles when you target x64 because it blissfully has only one calling convention.

修复:

template<class R, class T0, class T1>
class TFunction<R (__stdcall *)(T0,T1)>
{
    // etc..
};

这篇关于__stdcall 函数指针的模板部分特化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆