gpt4 book ai didi

c++ - __stdcall 函数指针的模板偏特化

转载 作者:可可西里 更新时间:2023-11-01 17:29:35 24 4
gpt4 key购买 nike

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 修饰符存在问题。这是编译器错误吗?

最佳答案

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

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

修复:

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

关于c++ - __stdcall 函数指针的模板偏特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5991507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com