为什么我必须将int *
更改为typedef int * IntPtr
才能编译?const int *&vs typedef int * IntPtr
template <class T>
class A
{
public:
template <class X>
void a(X *x, void (X::*fun)(const T&))
{
}
};
typedef int * IntPtr;
class B
{
public:
B() : a()
{
a.a(this, &B::foo); // this won't work
}
void foo(const int *&) // must replace `int *` here with `IntPtr`
{
}
A<int *> a; // ...and here
};
class C
{
public:
C() : a()
{
a.a(this, &C::foo);
}
void foo(const IntPtr&)
{
}
A<IntPtr> a;
};
我明白为什么typedefs是有用的,但不是为什么他们是必需的。类C
编译好B
没有。
这是MSVC++ 2008编译器编译错误:
Error 1 error C2784: 'void A<T>::a(X *,void (__thiscall X::*)(const T &))' : could not deduce template argument for 'void (__thiscall X::*)(const T &)' from 'void (__thiscall B::*)(const int *&)'
你可以发布的代码,因为它不会编译? – Hbcdev 2012-08-02 13:32:44
我会检查这段代码是否会重现问题(它应该)。我目前正在使用MSVC 2008编译器。 – 2012-08-02 13:33:58
'do'是一个关键字! – MvG 2012-08-02 13:37:38