Rvalue

First come a few quick questions and answers:
the right values had appeared? Rvalue already been made, and right until the reference value of c ++ 11 have
the right reference value what's the use? To reduce the copy.
Why rvalue references can reduce the copy? When the right value is assigned to the right value of the reference variable, direct address corresponding to the value of the right to the right reference variable value, equivalent to the corresponding memory for a vest.
When a reference value with the right? It is generally converted into a value of the left and right value, and a reference value assigned to the right to reduce the copy.

In order to distinguish between right and left values, look at a few examples from CSDN:

int A = 10;
int B = 20 is;
int * with pFlag = & A;
Vector <int> vctTemp;
vctTemp.push_back (. 1);
String str1 = "Hello";
String str2 = "World";
const int & m =. 1;

Will , a, b, a + b , a ++, ++ a, pFlag, * pFlag, vctTemp [0], 100, string ( "hello"), str1, str1 + str2, m values are the left or right value?
a and b are persistent objects (which may take its address), the value is left;
a + b is a temporary object (not take its address), a right value;
a ++ a persistent object is to remove the copy, then make a persistent object added to the value 1, and returns share copies, copies are temporary objects share (not take its address), so the value is a right side;
++ a persistent object is to make the value of a plus 1, and a persistent object that returns itself (which may take its address), so the value is left;
with pFlag and * pFlag are persistent objects (which may take its address), the value is left;
vctTemp [0] called heavy contained in [] operator, the operator [] returns a int &, for persistent objects (which may take its address), the value is left;
100 and string ( "hello"


m is a constant reference, a reference to the right value, but a persistent object references itself (which may take its address), L-values.

Rvalue reference variables declared &&
int X = &&. 3;
. 3 is not a constant value here, it should be regarded as a temporary value.

Const lvalue reference value can only get very much of the left, if the right amount to get a great value, this may be the right value has been stolen, will not do; and constants can not be assigned to a const reference, no matter about the value will not work.
Constant lvalue reference can get any value.
Const rvalue references can only get so much of the right value, if the right to obtain a constant value, this value will be the right to steal, no; and constants can not be assigned to a const reference, no matter about the value will not work.
Constant rvalue reference only get the right value constant or very much of the right value, if left to obtain a value, this value will be left will be stolen, no.

In the constructor of the class of
constant values that can be bound to the constant reference, it can not bind to the const reference.
Very preferentially bind to the magnitude of the const reference.
Left priority value is bound to the reference value on the left and right values to bind preferentially to the reference value on the right.

Rvalue reference value of the variable itself is left, how to assign him to the right reference value it? Use the move function is converted into the right value. Left value is no longer used in a move function may be converted to the right value.
int a =. 1;
int && X = STD :: Move (a);
where a left value, is converted into the right value is assigned to the right value of the reference
function in the right value is assigned to an rvalue reference variable, this variable is left value, if it is left to the value in this variable, if you still want to keep the type parameter, you need to use perfect forward std :: forward ()

下面是一个万能函数包装器
template<class Function, class... Args>
inline auto FuncWrapper(Function && f, Args && ... args) -> decltype(f(std::forward<Args>(args)...))
{
//typedef decltype(f(std::forward<Args>(args)...)) ReturnType;
return f(std::forward<Args>(args)...);
//your code; you can use the above typedef.
}

Guess you like

Origin www.cnblogs.com/litandy2016/p/12297585.html