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

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.

主站蜘蛛池模板: 定陶县| 阜阳市| 宜宾县| 饶阳县| 亚东县| 长宁县| 河曲县| 惠来县| 望城县| 特克斯县| 扎囊县| 灵宝市| 木里| 武穴市| 清河县| 揭阳市| 东莞市| 张家川| 绍兴县| 三门县| 湖州市| 瑞昌市| 申扎县| 阜城县| 阿瓦提县| 肥乡县| 五台县| 东丽区| 汾阳市| 衡水市| 固安县| 永年县| 沙坪坝区| 滦南县| 鄢陵县| 沁阳市| 汾阳市| 名山县| 铜鼓县| 和平县| 卢氏县|