C++ Logo

std-proposals

Advanced search

Re: = default { body... } on constructors and operator=

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Wed, 24 Feb 2021 15:45:01 -0500
On Wed, Feb 24, 2021 at 10:44 AM Bengt Gustafsson via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> It seems rather common that move constructors and move assignment
> operators need to first move all members and then just set one member in
> the source to indicate that it has been moved from. Couldn't we allow a
> body after = default to handle this situation.
>
Use the Rule of Zero. Make that last member a `HasBeenMovedFromFlag
m_flag`, whose move-constructor is singly responsible for handling that
logic; and then =default (or omit) your Big Five special members.

Just last week I "discovered" the following quirk. Here are three different
ways you can write a copy constructor:

struct A {
    static int counter;
    int i = ++counter;
    A(const A& rhs) = default; // #1: does not ++counter
    A(const A& rhs) : i(rhs.i) {} // #2: does not ++counter
    A(const A& rhs) { i = rhs.i; } // #3: DOES ++counter !!
};

This is perfectly explainable, of course; but it's still pretty unsettling.
Anything we can do to *not* make the situation more confusing, will be
appreciated.

–Arthur

Received on 2021-02-24 14:45:16