Search code examples
c++constructorc++11rvalue-reference

C++ constructor with rvalue reference


Consider this class with three constructors:

class Circle {

public:
 Circle(int r) {
      _radius = r;
 }

Circle(const Circle& c){
    _radius = c.radius();
    cout << endl << "Copy constructor with lvalue reference. Radius: " << _radius;
}

Circle(Circle&& c){
    _radius = c.radius();
    cout << endl << "Copy constructor with rvalue reference. Radius:" << _radius;
}

int radius() const {
    return _radius;
}

private:
    int _radius;
};

int main() {
     Circle c1(2);
     Circle c2(c1);
     cout << endl << c2.radius(); 
     Circle c3(Circle(4));
     cout << endl << c3.radius(); 
     return 0;
 }

Compiled with "g++ -std=c++0x". The output is:

Copy constructor with lvalue reference. Radius: 2
2
4

OK. The right constructors for the first two cases are called. But for the third case i.e., Circle c3(Circle(4)), I'd expect the third constructor, (copy constructor with rvalue referecne) to be called but it's not the case. Obviously some constructor is called since c3 is properly instantiated but I don't understand why the compiler is not using the explicitly provided one. Am I missing something here?


Solution

  • In order to take the rvalue reference, it should be non-const, since the contents of the constructor argument will be moved and typically this is an operation that changes the state of the operand (although not in your particular case):

    Circle(Circle&& c){ }
    

    Also, you are seeing a copy elision here:

    Circle c3(Circle(4));
    

    so the move constructor doesn't get invoked. This is a standard compiler optimization that may or may not happen. If you were to construct a Circle like this:

    Circle c3(std::move(c1));
    

    then you would invoke the move constructor.