Feb 9, 2012

[c++11][NOTE] Perfect Forwarding

Perfect Forwarding:
  • Applicable only to function templates.
  • Preserves arguments’ lvalueness/rvalueness/constness when
    forwarding them to other functions.
  • Implemented via std::forward.

------

Perfect forwarding isn’t really perfect. There are several kinds of arguments that cannot be

perfectly forwarded, including (but not necessarily limited to):
  • 0 as a null pointer constant.
  • Names of function templates (e.g., std::endl and other manipulators).
  • Braced initializer lists.
  • In-class initialized const static data members lacking an out-of-class definition.
  • Bit fields.
For details consult the comp.std.c++ discussion, "Perfect Forwarding Failure Cases"

referenced in the Further Information section of the course.


  • unnamed lvalue references are lvalues, and unnamed rvalue references are rvalues.
  • named lvalue references and named rvalue references are lvalues.

This is how std::forward works when this function return T&&!!


References:
Rvalue Reference Declarator: && [MSDN]
Forward template [MSDN]
How to: Write a Move Constructor [MSDN]
Rvalue References and Perfect Forwarding in C++0x [Just software]

No comments:

Post a Comment

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