C++: Perfect Forwarding

Link:

Rvalue References and Perfect Forwarding in C++0x (https://www.justsoftwaresolutions.co.uk/cplusplus/rvalue_references_and_perfect_forwarding.html)

摘要:

std::forward is designed for use in a template function which takes its arguments by T&&, where T is a template parameter.

std::forward preserves the rvalue-ness of the arguments, so if your function was called with an rvalue then std::forward<T> provides an rvalue. If your function was called with an lvalue then std::forward<T> provides an lvalue.

This works because of the way T is deduced from a T&& parameter: T is deduced to be "X&", for an lvalue argument of type X, whereas it is deduced to be plain "X" for an rvalue argument.

 

posted @ 2016-09-12 20:03  鱼悠游  阅读(308)  评论(0编辑  收藏  举报