Explicitly deleted move constructor
Asked Answered
B

1

6

Why this:

struct A
{
    A(int) {
        cout << "construct from int" << endl;
    }

    A(A&&) = delete;

    A(const A &) {
        cout << "copy constructor" << endl;
    }
};

int main(){
    A a = 0;
}

gives me an error:

error: use of deleted function ‘A::A(A&&)’

And why when I add such move constructor

A(A&&) {
    cout << "move constructor" << endl;
}

it compiles fine, but programm's output is just

construct from int

So as far as I understand, compiler asks for constructor but doesn't use it. Why? This makes no sense to me.

P.S. I assume that

A a = 0;

is equvalent of

A a = A(0);

but why neither move constructor nor move assignment operator is called?

Bilge answered 14/10, 2017 at 22:6 Comment(5)
Which compiler are you using?Guayaquil
This changed in C++17 (the deleted move constructor compiles now), but copy/move elision was always a thing before that.Persuasion
I tried it on g++4.9, g++6.3 and on clang 5.0 all same. (-O0 -std=c++11)Bilge
Indeed, compiles on clang 5 with -std=c++17Bilge
And now for some unsolicited advice: youtube.com/watch?v=vLinb2fgkHk&t=55m51sSibbie
R
4

According to the C++ Standard (12.8 Copying and moving class objects)

31 When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy/move operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.122 This elision of copy/move operations, called copy elision, is permitted in the following circumstances (which may be combined to eliminate multiple copies): .... — when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move

and

30 A program is ill-formed if the copy/move constructor or the copy/move assignment operator for an object is implicitly odr-used and the special member function is not accessible (Clause 11). [ Note: Copying/moving one object into another using the copy/move constructor or the copy/move assignment operator does not change the layout or size of either object. —end note ]

Rugby answered 14/10, 2017 at 22:25 Comment(2)
I find it's very hard to understand the Standard, can you give some explaination or sample code ?Caudle
@HảiPhạmLê It is enough to read the bolded text. And a sample code is given in the question.Rugby

© 2022 - 2024 — McMap. All rights reserved.