I can't figure out what I am doing wrong. Problem in the template deduction from pointer to base-class member - &DerivedClass::BaseClassMemeber.
Full example:
#include <vcl.h>
#include <tchar.h>
struct Base
{
int BaseClassMember;
};
struct Derived : public Base
{
};
template<class T1, class T2>
void Test(T1& Param1, T2 T1::* Param2)
{
}
int _tmain()
{
Derived inst;
// Compile error E2285 Could not find a match for 'Test<T1,T2>(B,int A::*) - BCC32
// Error 1 error C2782: 'void Test(T1 &,T2 T1::* )' : template parameter 'T1' is ambiguous - MS VS8
Test(inst, &Derived::BaseClassMember);
// Works great
Test<Derived>(inst, &Derived::BaseClassMember);
return 0;
}
I can found several workarounds, e.g. Additional Test function overloading with one more template parameter, static_cast, implicit partial specialization (Test).
But i am interested in reasons why complier just can't use class which was explicitly specified in &DerivedClass::BaseClassMemeber. That is the question. And if you have more elegant solution for problem, welcome.
Test<Base>
, however, does. – WhozCraigtemplate<class T1, class T2, class T3> typename std::enable_if<std::is_convertible<T2 T3::*, T2 T1::*>::value, void>::type Test(T1& Param1, T2 T3::* Param2)
, or if C++11 is an option,template<class T1, class T2, class T3> auto Test(T1& Param1, T2 T3::* Param2) -> decltype(Param1.*Param2, void())
. They will help you if you add additional overloads ofTest
, where you want overload resolution to ignore invalid attempts calls to this particularTest
(Test(inst, &Unrelated::ClassMember);
). – user743382