Why is it that I have to change the int * to a typedef int * IntPtr for this to compile?
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;
};
I understand why typedefs are useful but not why they are required. the class C compiles fine B does not.
This is the error from MSVC++ 2008 compiler:
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 *&)'
const int*&andtypedef int* IntPtr; const IntPtr&are not the same. In the first case it’s the int that’s constant, in the second case it’s the pointer. Only the second case is compatible with your template.If you write
instead, it should compile and work just fine.