Is the following move constructor code safe?

后端 未结 3 1419
走了就别回头了
走了就别回头了 2020-12-31 02:56

This is the move constructor of class X:

X::X(X&& rhs)
    : base1(std::move(rhs))
    , base2(std::move(rhs))
    , mbr1(std::move(rhs.         


        
3条回答
  •  攒了一身酷
    2020-12-31 03:32

    No, that would be an example of possible undefined behavior. It might work a lot of the time, but it's not guaranteed to. C++ allows it, but you are the one who has to make sure you don't try to use rhs again in that way. You are trying to use rhs three times after its guts had been ripped out its rvalue reference was passed to a function that might potentially move from it (leaving it in an undefined state).

提交回复
热议问题