__stdcall函数指针的模板部分专用化
Template partial specialization for __stdcall function pointer
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:projectorwellheadmultimapwizardtrunkexternals.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..
};
这是因为 (*) 表示默认调用约定,即 __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);
};
当然,这与 Win32 上不会忽略__stdcall
的R(__stdcall *)(T0, T1)
不匹配。如果要部分专用于函数指针,则需要为要接受的每个调用约定提供部分规范。
您没有针对 stdcall 情况专门化您的模板,即您需要
template<class R, class T0, class T1>
class TFunction<R(__stdcall *)(T0,T1)>
{
typedef R (*func_type)(T0,T1);
};
不确定语法,未经测试,但这应该是问题所在。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 调用专用模板时出错"no matching function for call to [...]"
- 模板专用化(按容器):value_type
- 如何用RISC-V GD32VF103CBT6开发板卸载精确的ADC过采样
- 如何将部分流作为参数传递
- 静态数据成员模板专用化的实例化点在哪里
- 特征 3 类的模板专用化
- Visual Studio 2017 不允许我创建 C++ 专用模板
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 字符串化递归的"std::vector<std::vector<...>>"而不使用部分模板函数专用化
- 具有常量引用参数的函数模板专用化
- 使用其他模板的模板专用化
- 使用专用显卡进行 OpenGL 渲染时帧速率较低
- 使用类型特征的部分类专用化
- 将 CRTP 与部分类专用化结合使用?
- 针对多种类型的部分类模板专用化
- 参数包不在最后位置的部分类模板专用化
- 具有多个参数的部分类模板专用化
- 函数指针类型和值的部分类专用化
- 带有映射的部分类模板专用化