The use of "c++11" perfect forwarding __c++

Source: Internet
Author: User


The perfect Forwarding (perfect forwarding) problem is the question of how the function template retains the left and right values of the parameter when it passes arguments to other functions.

That is, when a function template passes its own formal parameters to another function, it should be forwarded as a left value if the argument is left, and it should be forwarded to the right value if the corresponding argument is the right value.

This is done in order to preserve the possibility that the left and right values of other functions for the forwarded parameter are handled differently, such as when the parameter is a left-hand value, and the mobile semantics are enforced when the parameter is the right-hand value.

If you forward the values of your own arguments to the left values, the other functions can only treat the forwarded arguments as left values, thereby losing the possibility of different processing of the left and right values for the parameter.

An example of a perfect forwarding

[CPP]  View plain  copy   #include   "stdafx.h"    #include  <iostream>   & nbsp;  using namespace std;      void fun (int &x)  {  cout <<  "Lvalue ref"  << endl; }   Void fun (int  &&x)  { cout <<  "Rvalue ref"  << endl; }   Void fun (const int &x)  { cout <<  "Const lvalue ref"  << endl; }   Void fun (const int &&x)  { cout  <<  "Const rvalue ref"  << endl; }      template<typename t>   Void perfectforward (t &&t)  { fun (std:: Forward<t> (T)); }      Int _tmain (int argc, _tchar* argv[))    {       perfectforward (;         )   // rvalue ref          int a;       perfectforward (a);             // lvalue ref       perfectforward (Std::move (a)); //  rvalue ref          const int b = 8;        perfectforward (b);             // const lvalue ref       perfectforward (Std::move (b)) ; // const rvalue ref          system ("pause");        return 0;  }  

As you can see, the left and right values properties are perfectly preserved. Its core is Std::forward this template function.

About the perfect forwarding more detailed in-depth, you can look at this blog, wrote very detailed:

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: 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.