Move Semantics for POD-ish types

前端 未结 3 1244
隐瞒了意图╮
隐瞒了意图╮ 2021-02-20 04:31

Is there any point implementing a move constructor and move assignment operator for a struct or class that contains only primitive types? For instance,

struct Fo         


        
3条回答
  •  梦谈多话
    2021-02-20 05:12

    Even if you have that std::string member, it doesn't make sense to implement a move constructor. The implicit move constructor will already move each of the members, which in the case of float will just copy it, and in the case of std::string will move it.

    You should only really need to provide a move constructor when your class is doing some of its own memory management. That is, if you're allocating memory in the constructor, then you'll want to transfer the pointer to that allocated memory during a move. See the Rule of Five.

    It's possible to avoid this situation entirely if you always rely on smart pointers to handle your allocated memory for you. See the Rule of Zero.

提交回复
热议问题