[cfe-dev] deprecating copy construction and assignment

Howard Hinnant hhinnant at apple.com
Wed Mar 28 11:28:00 PDT 2012


On Mar 28, 2012, at 2:14 PM, David Blaikie wrote:

> On Wed, Mar 28, 2012 at 11:08 AM, Howard Hinnant <hhinnant at apple.com> wrote:
>> In a few instances the implicit definition of a defaulted copy constructor and copy assignment operator is deprecated in C++11.  E.g.:
>> 
>> struct A
>> {
>>  // A(const A&) = default;  // deprecated
>> 
>>  // A& operator=(const A&) = default;  // deprecated
>> 
>>  ~A();
>> };
>> 
>> Should we warn when both -std=c++11 and -Wdeprecated is given?
> 
> I believe we should, yes - just that no one's gotten around to
> implementing this (it's been somewhere on my mental list).
> 
> Aside: It'd be rather nice if these rules actually implemented the
> classic "rule of three" (if you implement any of the three you should
> implement all three - or 5 in C++11) but I don't think the standard
> wording goes quite that far.

It comes pretty close to the rule of 3:

The implicitly default copy constructor is deprecated if there is a user-declared copy assignment or destructor.

The implicitly default copy assignment is deprecated if there is a user-declared copy constructor or destructor.

One needs to be more careful with defaulting the move members.  It is easy to default them and have them get implicitly deleted:

struct member
{
    member();
    member(const member&);
    member& operator=(const member&);
    ~member();
};

struct A
{
    member m_;

    A() = default;
    A(const A&) = default;
    A& operator=(const A&) = default;
    A(A&&) = default;
    A& operator=(A&&) = default;
    ~A() = default;
};

A is neither move constructible nor move assignable.  The following is probably what was intended:

struct A
{
    member m_;

    A() = default;
    A(const A&) = default;
    A& operator=(const A&) = default;
    ~A() = default;
};

which, ironically, *is* move constructible and move assignable. :-)  But I'm not suggesting a warning for this.

Howard




More information about the cfe-dev mailing list