0

How is the move constructor implemented by the compiler?

Let's say I hae this class:

class Widget
{
    public:
        int a_;
        int b_;
    
    Widget(int a, int b):a_(a), b_(b){}
    
};

Is this how the move constructor is implemented by the compiler:

Widget(Widget&& other)
{
    a_ = std::move(other.a);
    b_ = std::move(other.b);
    
}

or is it like this:

Widget(Widget&& other)
{
    a_ = other.a;
    b_ = other.b;        
}
  • 2
    The synthesized move ctor does memberwise move like `Widget(Widget&& other): a_(other.a_), b_(other.b_){}`. Also see [`std::move` doesn't actually move anything](https://stackoverflow.com/questions/3413470/what-is-stdmove-and-when-should-it-be-used/27026280#27026280). – Jason Aug 29 '22 at 08:12
  • 2
    you cannot move an `int`, moving an `int` is copying it. It makes no difference wether the compiler would synthesize the first of the second version – 463035818_is_not_an_ai Aug 29 '22 at 08:14
  • 1
    [Do built-in types have move semantics?](https://stackoverflow.com/questions/14679605/do-built-in-types-have-move-semantics) – Jason Aug 29 '22 at 08:18

0 Answers0