Translation "c++ Rvalue References explained"c++ rvalue Reference detailed Part7:perfect Forwarding (perfect forwarding): question

Source: Internet
Author: User

For the seventh part of this document, please refer to the overview section: http://www.cnblogs.com/harrywong/p/cpp-rvalue-references-explained-introduction.html.

Perfect Forwarding (Perfect forwarding): problem

Another problem that the rvalue reference behind the move semantics is used to solve is the perfect forwarding problem. Consider the following simple factory functions:

Template<typename T, TypeName arg> shared_ptr<T> Factory (Arg arg) {   return Shared_ptr<t> (new  T (ARG));

Obviously, the purpose here is to pass the ARG parameter through the factory function to the constructor of T. Ideally, in the case of ARG, everything should behave without a factory function, the constructor is called directly by the client's code: that is, perfect forwarding. The above code unfortunately failed in this situation: it introduces an additional call to value, which is bad when the constructor uses a reference type as a parameter.

A more general solution, such as being used by Boost::bind, is to let the parameters of the outer function use reference types:

Template<typename T, TypeName arg> shared_ptr<T> Factory (
arg&
Arg) {   return shared_ptr<t> (new  T (ARG));}

It's better, but it's not perfect. The problem is that the current factory function cannot be called by the right value:

// error: If Hoo returns factory<x> by value (a//  error

This can be resolved by providing a const reference parameter Overload:

Template<typename T, TypeName arg> shared_ptr<T> Factory (
Arg Const &
Arg) {   return shared_ptr<t> (new  T (ARG));}

There are two problems with this method. First, if factor is more than just one, but with a number of parameters, you need to provide all the combined overloads of the Non-const and const reference parameters. As a result, this solution is poorly scalable when confronted with many parameters to the function.

Second, this method is not perfect because it blocks the move semantics: the parameter of the T constructor in the body of the factory function is an lvalue. Therefore, the move semantics never occur even if there is no external wrapper function.

It turns out that rvalue references can be used to solve such problems. It allows true perfect forwarding to be achieved without any overloads. To understand if it works, we need to take a look at the two rules that the rvalue references.

Translation "c++ Rvalue References explained"c++ rvalue Reference detailed Part7:perfect Forwarding (perfect forwarding): question

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.