Tag: Temp value exists str constructor cannot c++11 failure type
C++11 Standard library functions Std::move and perfect forwarding Std::forward
#define_crt_secure_no_warnings#include<iostream>#include<string>#include<vector>#include<map>//C + + also has a widely agreed to the argument that the address can be taken, the name is the left value, conversely, can not take the address, no name is the right value. //The rvalue, relative to the Lvalue, represents a literal constant, an expression, a non-reference return value for a function, and so on. //since the compiler only calls the transfer constructor and the transfer assignment function on rvalue references, all named objects can only be lvalue references, and if it is known that a named object is no longer in use and wants to call the transfer constructor and the transfer assignment function on it, that is, to use an lvalue reference as an rvalue reference, how do you do it? //The standard library provides the function std::move, which converts an lvalue reference to an rvalue reference in a very simple way. intA;int&&R1 = A;//Compilation failedint&&R2 = Std::move (a);//compiled by//Perfect Forwarding Std::forward//Perfect forwarding applies to scenarios where a set of parameters needs to be passed intact to another function.//"Intact" is not just the value of the parameter, but in C + +, there are two sets of properties in addition to the parameter values: Lvalue/Rvalue and Const/non-const. //Perfect forwarding means that all of these attributes and parameter values cannot be changed while the parameter is being passed, without additional overhead, as if the redirector were not present. In generic functions, such requirements are very common. //How does c++11 solve the problem of perfect forwarding? In fact, c++11 is the perfect forwarding by introducing a new language rule called "Reference folding" (reference collapsing) and combining the new template derivation rules. typedefConst intt;typedef T&tr;tr&v =1;//in c++11, once such an expression occurs, a reference collapse occurs, collapsing the complex unknown expression into a known simple expression/*The reference collapse rule in c++11: The type definition of TR declares the actual type of V for type T & TR T & T & TR & T & T & tr && T & T && tr t && t && TR & T & T && tr && T && once an lvalue reference appears in the definition, the reference collapse always takes precedence to collapse it to an lvalue reference*///c++11, Std::forward can save the left or right value attributes of a parameter#include <iostream>using namespacestd;template<typename t>voidProcess_value (T &val) {cout<<"T &"<<Endl;} Template<typename t>voidProcess_value (T &&val) {cout<<"T &&"<<Endl;} Template<typename t>voidProcess_value (ConstT &val) {cout<<"Const T &"<<Endl;} Template<typename t>voidProcess_value (ConstT &&val) {cout<<"Const T &&"<<Endl;}//The function forward_value is a generic function that passes one argument to another function Process_valueTemplate <typename t>voidForward_value (T && val)//argument is an rvalue reference{process_value (Std::forward<T> (Val));//c++11, Std::forward can save the left or right value attributes of a parameter}voidmytest () {intA =0; Const int&b =1; Forward_value (a); //T &Forward_value (b);//Const T &Forward_value (2);//T &&Forward_value (Std::move (b));//Const T && return;}intMain () {mytest (); System ("Pause"); return 0;}
C++11 Standard library functions Std::move and perfect forwarding Std::forward