Moving a template method to compiling derived interrupts

Given the following code:

template<typename T>
class A
{
public:
   T t;
};

class B
{
public:
   void foo(int i) {}

   template<typename T>
   void foo(A<T>& a) {}
};

int main()
{
   A<int> a;
   B      b;

   b.foo(a  );
   b.foo(a.t);
}

This compiles and works fine; The correct overloaded versions B::foo()are selected and invoked for aand a.t.

Now I am introducing a new class Cthat is obtained from Band moves the version of the template ::foo()from Band to C:

template<typename T>
class A
{
public:
   T t;
};

class B
{
public:
   void foo(int i) {}
};

class C: public B
{
public:
   template<typename T>
   void foo(A<T>& a) {}
};

int main()
{
   A<int> a;
   C      c;

   c.foo(a  ); // Fine
   c.foo(a.t); // Error
}

And now the code will no longer compile. Visual Studio 2005 states:

error C2784: 'void C::foo(A<T> &)' : could not deduce template argument for 'A<T> &' from 'int'

In fact, a call C::foo()with any value intresults in this error. It seems that the method overload for is inthidden by template overload.

Why is this happening? Is this some kind of problem with the Visual Studio 2005 compiler? Unfortunately, I cannot test it on any other compiler right now.

.

+5
3

, int .

! C:

class C: public B
{
 public:
  using B::foo;
  template<typename T>
  void foo(A<T>& a) {}
};

- , - . . §3.3.10/3 ISO/IEC 14882: 2011:

( 10) ; . 10.2.

+5

, .

class C: public B
{
public:
   using B::foo;
   template<typename T>
   void foo(A<T>& a) {}
};
+2

, . . using B::foo; C, .

+1

All Articles