C++ constructor with rvalue reference

12,366

Solution 1

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.

Solution 2

No move constructors are being called because your compiler is too smart for your code ;)

 Circle c1(2);

This just constructs an object with the int conversion constructor.

 Circle c2(c1);

This is a copy operation. c1 is an l-value, so it provokes a copy.

 Circle c3(Circle(4));

Here, your compiler recognizes that you're basically telling it to construct the object twice. So it elides one of the object constructors. This is allowed in this instance by the C++ specification.

If your compiler couldn't elide the construction, then it would perform an move. Also, if your compiler can't elide that, throw it away.

So there's no moving going on.

Share:
12,366
PoP
Author by

PoP

Updated on September 01, 2022

Comments

  • PoP
    PoP over 1 year

    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?