官术网_书友最值得收藏!

Lvalue references

Before understanding why rvalue references were introduced in the first place, let's clear things up regarding lvalues, references, and lvalue-references. When a variable is an lvalue, it can be addressed, it can be pointed to, and it has a scoped storage duration:

double pi{3.14}; // lvalue
int x{42}; // lvalue
int y{x}; // lvalue
int& ref{x}; // lvalue-reference

ref is an lvalue reference, a synonym for a variable that can be treated as a const pointer:

int * const ref = &x;

Besides the ability to modify the objects by a reference, we pass heavy objects to functions by reference in order to optimize and avoid redundant object copies. For example, the operator+ for the Warehouse takes two objects by reference, thus making it copy addresses of objects rather than full objects.

Lvalue references optimize the code in terms of function calls, but, to optimize temporaries, we should move on to rvalue references.

主站蜘蛛池模板: 石楼县| 札达县| 志丹县| 新疆| 丹东市| 蛟河市| 资中县| 博白县| 镇巴县| 上蔡县| 齐河县| 乌拉特前旗| 凉城县| 江津市| 金华市| 牙克石市| 抚州市| 乐亭县| 名山县| 临汾市| 雅江县| 子洲县| 崇义县| 乌海市| 海宁市| 遂昌县| 界首市| 江永县| 罗定市| 平塘县| 康定县| 龙门县| 大新县| 水富县| 富川| 准格尔旗| 门头沟区| 城固县| 辽中县| 龙游县| 马山县|