C++11 reference collapsing and perfect forward (reference folding and Perfect forwarding)

Source: Internet
Author: User

The reference folding principle and perfect forwarding are related, it can be said that the latter is based on the former characteristics of some of the implementation, specifically to look at.
To understand the perfect forwarding, you need to understand two knowledge points:
1. Reference folding principle (Reference collapsing rules).
2. Right value function template parameter type derivation (Template argument deduction) First look at the reference fold.

The so-called reference folding or collapsing (collapse) is both when the reference refers to the reference, where the reference can be a left or a right value reference, the final type will be partially folded together. The specific rules are as follows (note the spaces between the address characters):

before folding after folding Notes
a& & a& Left value reference to left value references collapse/collapse to left value reference
a& && a& Right value reference to left value references collapse/collapse to left value reference
a&& & a& Left value reference to right value reference collapse/collapse to left value reference
a&& && a&& Right value reference to right value references collapse/collapse as right value reference

The above principles can be summed up as follows: All the right-left value refers to the participation of the case, the final type will become a left-value reference, only if all of the right value reference to the case will become a right value reference. The derivation of function template parameter type

Note: for template parameter derivation, more can refer to the relevant terms of "effective modern C + +".
This is a special case of the function template parameter type derivation, which uses the template parameter as a right value reference, for example:

Template<typename t>
void foo (t&&);

Where T is the template type,t&& as the parameter type. This situation can produce two kinds of results:
1. When the parameter passed to the Foo function is a reference to a left value, for example:

int i =;
Foo (i);//i is a left-value reference

At this point, the type of T is a reference to the left value of int: Int&, the parameter type is int & &&, (both t&&), combined with the above reference-collapse rule, the final parameter's type is the value of the int's left reference:int&.
2. When the parameter passed to the Foo function is a reference to a right value, for example:

Foo (29);

At this point, the type of T is int, and the parameter type is Int&&, (both t&&).
With these rules, let's look at the perfect forwarding problem:
Have the following code:

Template<typename T, TypeName arg> 
shared_ptr<t> Factory (arg&& Arg)
{return 
  shared_ Ptr<t> (New T (Std::forward<arg> (ARG));

The definition of Std::forward is similar to this:

s&& forward (TypeName remove_reference<s>::type& a) noexcept
{return
  static_cast<s &&> (a);

Now we call the factory method with both a left reference and a right value reference: First Look at the left value reference:

x x;
Factory<a> (x);//x and A are custom types

At this point the ARG type is X&, so the above code translates to:

Shared_ptr<a> Factory (x& && Arg)
{return 
  shared_ptr<a> (new A (std::forward<x &> (ARG));
} 

x& && Forward (remove_reference<x&>::type& a) noexcept
{return
  static_cast<x & &&> (a);

After a reference is folded, it becomes:

Shared_ptr<a> Factory (x& Arg)
{return 
  shared_ptr<a> (new A (Std::forward<x&> (ARG) ));
} 

x& Std::forward (x& a) 
{return
  static_cast<x&> (a);

At this point, the left value reference is forwarded perfectly. then look at the case of the right value reference

x foo ();//foo () returns the X of the value type, both the right value.
factory<a> (foo ());

At this point the ARG type is x, so the above code translates to:

Shared_ptr<a> Factory (x&& Arg)
{return 
  shared_ptr<a> (new A std::forward<x> (ARG )));
} 

x&& forward (x& a) noexcept
{return
  static_cast<x&&> (a);

The right value reference is forwarded perfectly.
Matching with the Std::forward, there is a std::move is used from a left-value reference or a reference to the right value to produce a right value reference, this has the first two principles of the article, combined with the implementation of move code (in fact, is a static_ Cast) can be easily deduced, no longer repeat.

Reference: http://thbecker.net/articles/rvalue_references/section_08.html

Related Article

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.