mardi 9 juillet 2019

Program output explanation lvalue/rvalue references [duplicate]

This question already has an answer here:

I was playing around some C++ code about lvalue and rvalue found some strange behavior for me. Consider the following code:

#include <iostream>

class A
{
public:
    A() = default;
    A(const A&) { std::cout << "copy\n"; }
    A(A&&)      { std::cout << "move\n"; }
    A& operator=(const A&) = delete;
    A& operator=(A&&) = delete;
};


int main()
{
    A a;
    A b = a;             // prints "copy"
    //A b = std::move(a);  // prints "move"
    //A b = A();           // prints nothing

    return 0;
}

As mentioned in comments, A b = a; calles copy constructor and A b = std::move(a); calls move constructor, which is quite clear.

However in case of A b = A(); nothing is printed to console. How can that possible? As far as I understand it should call move constructor as we are dealing with rvalue. Explanations are welcomed!

Aucun commentaire:

Enregistrer un commentaire