Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was the std::pair class standard changed to disallow types with only a nonconstant copy constructor in C++11?

I am reading through Nicolai M. Josuttis' "The C++ Standard Library (Second Edition)" and have just reached the section on std::pair. The author notes that:

Since C++11, a pair<> using a type that has only a nonconstant copy constructor will no longer compile.

He then goes on to give the following example:

class A 
{
   public:
     ...
     A( A& ); // copy constructor with nonconstant reference
     ...
};

std::pair<A, int> p; // Error since C++11

However, I'm interested in the reason that the standards committee decided to make this amendment to the standard library standard? I tried to google the reason, but was unsuccessful in finding anything pertinent.

like image 243
Thomas Russell Avatar asked Oct 04 '22 02:10

Thomas Russell


1 Answers

In C++98, copy constructors with non-constant reference parameters would be (ab)used to "move" elements around. Such code was notoriously unsafe and unclear. The infamous and now deprecated std::auto_ptr was the prime example of that.

In C++11, we have move semantics and rvalue references to achieve the same effects much more safely and clearly. This is because an rvalue reference is a reference to a mutable object, but it can only bind to "safe" expressions such as temporaries or things that you have explicitly cast (via std::move) and thus marked as disposable.

In short: classes with copy constructors taking non-reference members have no real use cases that cannot be done better and safer with rvalue references. std::pair acquired a move constructor std::pair(std::pair&&)=default to accomodate such semantics.

like image 168
TemplateRex Avatar answered Oct 13 '22 11:10

TemplateRex