Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this move constructor not working

I have the following code snippet. Does anyone know why this move constructor is not called for all cases in the main function? Why does it compile anyway? Assignment operator is private? Here the link: http://ideone.com/bZPnyY

#include <iostream>    
#include <vector>

class A{
public:
    A(int i){
        std::cout << "Constructor "<< i <<std::endl;
        for(int l = 0; l<i;l++){
            vec.push_back(l);
        }
    };

    A(A && ref): vec(std::move(ref.vec))
    {
       std::cout << "Move constructor"<<std::endl;
    }

    A & operator=(A && ref){
       if(this != &ref){
            vec = std::move(ref.vec);
       }
       std::cout << "Move assignment"<<std::endl;
       return *this;

    }

    std::vector<int> vec;

private:
    A(const A & ref);
    A(A & ref);
    A & operator=(A & ref);
};


A makeA(){
    A a(3);
    return a;
}

int main(){
    A b1(makeA()) ;
    A b2 = makeA();
    A b3 = A(3);
    A b4(A(3));
    std::cout << b4.vec[2] << std::endl;
};

Output:
Constructor 3
Constructor 3
Constructor 3
Constructor 3
2

Some Additions to the responses: When I add

 std::pair<int,A> a(3,A(3));

Then the move constructor gets called (so no NRVO hopefully)

like image 985
Gabriel Avatar asked Jan 13 '23 08:01

Gabriel


1 Answers

What you are seeing is called copy elision and is documented in 12.8.31.

A makeA(){
    A a(3);
    return a;
}

A b1(makeA()) ;
A b2 = makeA();

Here, the a local variable in makeA is emplaced directly in the return value of makeA, and in turn the temporary return value of makeA is emplaced directly in the storage of b1 and b2. (two copy elisions in a row)

A b3 = A(3);
A b4(A(3));

The temporary created with A(3) is constructed directly in b3 and b4. (one copy elision)

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):

This is the first part of the b1 and b2 case:

  • in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object (other than a function or catch-clause parameter) with the same cv- unqualified type as the function return type, the copy/move operation can be omitted by constructing the automatic object directly into the function’s return value

This is the second part of the b1 and b2 case, as well as the whole part of the b3 and b4 case:

  • 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

There are also two other copy elision allowed cases to do with throwing and catching exceptions.

like image 128
Andrew Tomazos Avatar answered Jan 22 '23 18:01

Andrew Tomazos