Perfect forwarding - what's it all about? [dup

2019-01-21 04:59发布

Possible Duplicate:
Advantages of using forward

Could someone please explain to me what perfect forwarding is about?

2条回答
倾城 Initia
2楼-- · 2019-01-21 05:11

http://www.justsoftwaresolutions.co.uk/cplusplus/rvalue_references_and_perfect_forwarding.html

Why is this useful? Well, it means that a function template can pass its arguments through to another function whilst retaining the lvalue/rvalue nature of the function arguments by using std::forward. This is called "perfect forwarding", avoids excessive copying, and avoids the template author having to write multiple overloads for lvalue and rvalue references.

查看更多
做自己的国王
3楼-- · 2019-01-21 05:11

Quoting Session Announcement: Adventures in Perfect Forwarding:

Perfecting forwarding is an important C++0x technique built atop rvalue references. It allows move semantics to be automatically applied, even when the source and the destination of a move are separated by intervening function calls. Common examples include constructors and setter functions that forward arguments they receive to the data members of the class they are initializing or setting, as well as standard library functions like make_shared, which “perfect-forwards” its arguments to the class constructor of whatever object the to-be-created shared_ptr is to point to.

查看更多
登录 后发表回答