C++ - autogenerated copy constructor and assignment operator gotchas

It has been changed that the semantic of the copy constructor and the assignment operator in the relationship with class hierarhcy from vs10 to vs12...
one case of the gotchas is like this:

#include <iostream>
struct A
{
  A& operator=(const A&) { return *this; }
  template <typename T> A& operator=(const T&) // while one can argue that this is the desired semantic, B will continue to search its parent's definition if there is none provided 
  {
    std::cout << "This should not be printed" << std::endl;
    return *this;
  }
};

struct B : A { };



int _tmain(int argc, _TCHAR* argv[])
{
	  B x;
  B y;
  x = y;
 return 0;
}

 

 

You will see the output "This should not be printed" printed. but if you run the same code in VS 2012, you won't see it. 

if you change the main to this:

int _tmain(int argc, _TCHAR* argv[])
{
	 // B x;
  //B y;
  //x = y;

  A x ;
  A y ;
  y = x;
 return 0;

you will NOT see the output "This should not be printed" printed.

 

I guess it is because in VS2010, it is deemed right to go the parent's scope when resolving copy constructor or assignment operator before it auto generate the constructor or assignment operator... And now this has been changed because this is too dangerous?

 

 

你可能感兴趣的:(C++)