:
my_type x = func_returning_my_type_byvalue();
my_type & y = func_returning_my_type_byvalue();
my_type && z = func_returning_my_type_byvalue();
- x (rvalue), / x ( x func_returning_my_type_byvalue, ).
, x lvalue - , . , , . lvalues .
- ( ), lvalue. , :
my_type & y = func_returning_my_type_byreference();
// `y` will never use constructors or destructors
, -, . - func arg . , :
void func( my_type && arg ) {
my_type && save_arg = arg;
}
, , , arg . arg (, ) , save_arg, save_arg - , . , save_arg , lval, , func, - !
, std:move . func, , , , .
arg , my_type&, rvalue. - . , "rvalue".
, increment/decment. , . operator++(void) (pre) operator++(int) (post). int, , // . .
rvalue lvalue lvalue, ?
: .
lvalue - , , . , lvalue, , .
, , :
int a; // created first, destroyed last
int b; // created second, destroyed 2nd-last
int & c = b; // fine, `c` goes out of scope before `b` per above
int && d = std::move(a); // fine, `a` outlives `d`, same situation as `c`
rvalue, , lvalue, - lvalue , c d. std::move, - d - , , , rvalue / , .
, lvalue, , , , , , , , . .
rvalue, , . . / elision, , :
int a = 2, b = 3;
int && temp = a + b;
func
lvalue - , , , lvalues, rvalue.
:
func( std::move( variable ) ); // case 1
func( my_type() + my_type() ); // case 2
func , ( ). 1, rvalue , , , func , "" .
, , 1 , arg , func , - , arg , func , func - arg my_type&, my_type&&.