[英]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:\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..
};
這是因為(*)表示默認調用約定,即__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上的R(__stdcall *)(T0, T1)
不匹配R(__stdcall *)(T0, T1)
其中__stdcall
不被忽略。 如果你想部分專門用於函數指針,那么你需要為你想接受的每個調用約定提供一個部分規范。
您沒有專門為stdcall案例設置模板,即您需要
template<class R, class T0, class T1>
class TFunction<R(__stdcall *)(T0,T1)>
{
typedef R (*func_type)(T0,T1);
};
不確定語法,未經測試,但這應該是問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.