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

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.

主站蜘蛛池模板: 盖州市| 新巴尔虎左旗| 阳东县| 都昌县| 香港| 富民县| 乌兰浩特市| 正宁县| 互助| 洪泽县| 上蔡县| 增城市| 莱阳市| 湖口县| 亳州市| 湖口县| 阜新市| 镇巴县| 皮山县| 宁南县| 喜德县| 巴中市| 醴陵市| 上栗县| 苏州市| 沁源县| 景东| 湾仔区| 香河县| 崇信县| 琼海市| 朝阳县| 东海县| 高雄县| 苍山县| 石嘴山市| 微博| 宝丰县| 宁城县| 绿春县| 卓资县|