C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Enforce const correctness on deleted move constructor

From: Smith, Jim <jim.smith_at_[hidden]>
Date: Tue, 23 Jan 2024 14:30:08 +0000
Hi Jens,

I agree.

In the standard under 'Copy/move constructors' 11.4.5.3 it says "X(const X&&) // OK, but possibly not sensible". If you're moving I don't see how allowing this is logical.

During a code review I seen a deleted const rvalue ref constructor.

My assumption was that it didn't really make sense and wouldn't delete the usual non const move constructor but it did delete both.


- James S.

On Saturday, December 2nd, 2023 at 5:00 PM, Jens Maurer <jens.maurer_at_[hidden]> wrote:

>
> On 02/12/2023 21.46, Smith, Jim via Std-Proposals wrote:
>
> > Hi,
> >
> > Example:
> > class A {
> > public:
> > ...
> > A(const A&&) = delete ;
> > ...
> > };
> >
> > The above deleted move constructor with const signature should not delete the default move constructor with non-const signature A(A&&) as the compiler error says for the following code:
> >
> > A x;
> > A a = std::move(x);
> >
> > *compiler: *note: 'A' has been explicitly marked deleted here
> > A(const A&&) = delete;
> >
> > In this case an implicit default non-const move constructor should be called unless it's also deleted.
>
>
> If you feel the need to delete const rvalue ref constructors,
> why can't you just declared and = default the non-const rvalue
> constructor (i.e. the usual move constructor)?
>
> Jens

Received on 2024-01-23 14:30:20