为什么我需要将
int *
更改为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;
};
我了解为什么typedef很有用,但不理解为什么它们是必需的。类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 *&)'