C++11 Move semantics behaviour specific questions

↘锁芯ラ 提交于 2019-12-06 06:50:40

Does copy elision and RVO would still work for classes without move constructors?

Yes, RVO still kicks in. Actually, the compiler is expected to pick:

  • RVO (if possible)
  • Move construction (if possible)
  • Copy construction (last resort)

Why can't STL internally leverage move semantics to improve such operations internally using operations like copy elision or RVO which doesn't require move constructors?

The STL containers are movable, regardless of the types stored within. However, operations on the objects in the container require the object cooperation, and as such sort (for example) may only move objects if those objects are movable.

Do we get benefited by move semantics in below case [...] as integer is a primitive type ?

Yes, you do, because containers are movable regardless of their content. As you deduced, st2 will steal the memory from st1. The state of st1 after the move is unspecified though, so I cannot guarantee its storage will have been nullified.

When a push_back() is called using std::move on lvalue [what happens] ?

The move constructor of the type of the lvalue is called, typically this involves a bitwise copy of the original into the destination, and then a nullification of the original.

In general, the cost of a move constructor is proportional to sizeof(object); for example, sizeof(std::string) is stable regardless of how many characters the std::string has, because in effect those characters are stored on the heap (at least when there is a sufficient number of them) and thus only the pointer to the heap storage is moved around (plus some metadata).

  1. Yes.
  2. They do, as far as possible.
  3. Yes. std::vector has a move constructor that avoids copying all the elements.
  4. It is still in contiguous.

e.g.

struct MyClass
{         
     MyClass(MyClass&& other) 
         : xs(other.xs), size(other.size)
     {
          other.xs = nullptr;
     }

     MyClass(const MyClass& other) 
       : xs(new int[other.size]), size(other.size)
     { 
         memcpy(xs, other.xs, size);
     }

     ~MyClass() 
     {               
         delete[] xs;
     }

     int* xs;
     int size;
}

With a move constructor only xs and size needs to be copied into the vector (for contiguous memory), however we do not need the perform memory allocation and memcpy as in the copy constructor.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!