Jul 14, 2014

[C++11] std::move / std::forward

Quoted from Effective Modern C++:

  • Neither std::move nor std::forward do anything at runtime.
  • Apply std::move to rvalue references and std::forward to universal references the last time each is used.
  • Do the same thing for rvalue references and universal references being returned from functions that return by value.
  • Never apply std::move or std::forward to local objects (including by-value parameters) if they would otherwise be eligible for the return value optimization.
  • Overloading on universal references almost always leads to the universal reference overload being called more frequently than expected.
  • Perfect-forwarding constructors are especially problematic, because they’re typically better matches than copy constructors for non-const lvalues, and they can hijack derived class calls to base class copy and move constructors.
  • Alternatives to the combination of universal references and overloading include the use of distinct function names, passing parameters by (lvalue)-reference-to-const, passing parameters by value, and using tag dispatch.
  • Universal reference parameters often have efficiency advantages, but they also have usability disadvantages.
  • Reference collapsing occurs in four contexts: 
    • template instantiation, 
    • auto type generation, 
    • creation and use of typedefs and alias declarations,
    • decltype.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.