Original http://blog.csdn.net/aqtata/article/details/35372769
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: http://www.cnblogs.com/hujian/archive/2012/02/17/2355207.html