문제

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.

도움이 되었습니까?

해결책

&Derived::BaseClassMember has type int Base::*, not int Derived::*. Quoth the Standard:

The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualified-id. If the operand is a qualified-id naming a non-static member m of some class C with type T, the result has type “pointer to member of class C of type T” and is a prvalue designating C::m. [... skip ...] [Example:

struct A { int i; };
struct B : A { };
... &B::i ...             // has type int A::*i

— end example]

You have to cast the value to int Derived::* if you need this type.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top