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)