0

I've run some example programs and noticed that move functions (constructor and assignment operator) never used until i compile with disabled copy elision (in GCC "-fno-elide-constructors"). So the questions are:

  • When in practice (with modern mainstream C++11/14 or C++17 compilers) move functions are useful?
  • If move functions required, when user implemented move functions needed instead of default compiler-generated ones?

P.S. AFAIK RVO is mandatory in C++17 standard, but even in not latest versions of C++11/14 compilers RVO/NRVO implemented (GCC version 5.3).

morte
  • 325
  • 1
  • 10
  • 1
    Try to put a class into a vector. Resize it, delete some values form the middle of the vector. Insert some values into the middle of the vector. The vector will use move semantics to shuffle its contents around. – Sam Varshavchik Apr 22 '18 at 13:14
  • Remember to make your move constructor and move assignment operator `noexcept` - otherwise `std::vector` (and others) will not use them and fall back to copying. – Jesper Juhl Apr 22 '18 at 13:27
  • Most compilers have RVO & NRVO implemented. In practice, move functions MAY be shown to be useful when you profile. If you don't care enough about performance to profile, you probably don't actually care enough about the performance gain of move functions -- it's a tradeoff of more code versus (potentially) more performance. – Eljay Apr 22 '18 at 13:29
  • Though copy elision is not exactly RVO, the question is essentially the same. – xskxzr Apr 22 '18 at 17:34

0 Answers0