T(T&)
isn't new in C++11. What's new is the use of the default
keyword to explicitly define the automatically generated copy constructor:
T(const T &) = default;
As your link says:
A class can have multiple copy constructors, e.g. both T::T(const T&)
and T::T(T&)
. If some user-defined copy constructors are present, the user may still force the generation of the implicitly declared copy constructor with the keyword default
.
Before C++11, if you had a T(T&)
constructor in your class, it would prevent the automatic creation of T(const T&)
(the default copy constructor). Since C++11 you can have both (a user defined T(T&)
constructor and an automatic T(const T&)
constructor).
As for what a non-const copy constructor is useful for, the answer is "not much". It ensures that only mutable objects can be copied (and not e.g. temporaries), which is not something you usually need.
(Probably it was used to implement move semantic???)
That's exactly right. See e.g. auto_ptr
(now removed from the C++ standard), which was similar to unique_ptr
before we had move semantics. In particular, it had a auto_ptr(auto_ptr &r)
constructor that would modify r
(which lead to somewhat unintuitive behavior, which is why it was removed from the language).