This page looks best with JavaScript enabled

[EMCpp]Item-29 Assume Move Operations Are Not Present Not Cheap, and Not Used

 ·  ☕ 1 min read · 👀... views

There are several scenarios in which C++11’s move semantics do us no good:

  • No move operations: the object to be moved from fails to offer move operations. The move request thus becomes a copy request
  • Move not faster: the object to be moved from has move operations that are no faster than its copy operations1
  • Move not usable: the context in which the moving would take place requires a move operation that emits no exceptions, but that operation isn’t declared noexcept
  • Source object is lvalue: with very few exceptions (e.g., item 25), only rvlaues may be used as the source of a move operation

  1. For example, std::vector, conceptually, holds only a pointer to the heap memory storing the contents of the container, so it is possible to move the contents of an entire container in constant time; however, for std::array, the data is stored directly in the std::array object, so the move operation runs in linear time. Similar analysis applies to std::string when the small string optimization (SSO) occcurs. ↩︎

Share on
Support the author with