Cam*_*ron 6 c++ templates member-function-pointers member-functions template-argument-deduction
我有这个示例代码:
struct A
{
int foo() { return 27; }
};
template<typename T>
struct Gobstopper
{
};
template<>
struct Gobstopper<int(void)>
{
Gobstopper(int, int) { } // To differentiate from general Gobstopper template
};
template<typename ClassType, typename Signature>
void DeduceMethodSignature(Signature ClassType::* method, ClassType& instance)
{
// If Signature is int(), Gobstopper<> should resolve to the specialized one.
// But it only does on x64!
Gobstopper<Signature>(1, 2);
}
int main(int argc, char** argv)
{
A a;
DeduceMethodSignature(&A::foo, a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译好g++.它也适用于VC10,但仅适用于64位平台.当我为32位平台构建时,我得到了这个编译错误:
error C2661: 'Gobstopper<T>::Gobstopper' : no overloaded function takes 2 arguments
1> with
1> [
1> T=int (void)
1> ]
1> c:\...\test.cpp(26) : see reference to function template instantiation 'void DeduceMethodSignature<A,int(void)>(Signature (__thiscall A::* ),ClassType &)' being compiled
1> with
1> [
1> Signature=int (void),
1> ClassType=A
1> ]
Run Code Online (Sandbox Code Playgroud)
该错误表明正在使用Gobstopper的非专用版本,这必然意味着Signature其他东西int (void).但错误也明确说,Signature 是 int (void).那么错误来自哪里?我该如何解决?
我唯一能想到的可能是从32位变为64位并且没有显示在错误消息中显示的签名中是调用约定; 显然,VC x64有一个统一的调用约定,而对于x86,每个调用约定都是不同的.但即使这是问题,我也不知道如何解决它.
编辑:我应该提到我尝试使用常规(非成员)函数指针,并且工作正常.
你是对的.SignatureWin32目标的类型是int __thiscall(void)在x64上int __cdecl(void).请注意,在任一目标上,通常调用的非成员函数的类型int(void)确实int __cdecl(void)如此,巧合的是,其中一个构造类型实际上(不是真正正确!)匹配.
一般来说,不建议通过模板魔法混合不同类型的函数指针,因此Gobstopper专业化应该看起来像int (ClassType::*)().
| 归档时间: |
|
| 查看次数: |
477 次 |
| 最近记录: |