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

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.

主站蜘蛛池模板: 池州市| 南丰县| 怀柔区| 桐乡市| 五华县| 兰西县| 班戈县| 诸城市| 太保市| 宜川县| 宁城县| 北碚区| 探索| 临汾市| 临夏县| 江阴市| 黄平县| 龙陵县| 昌图县| 杭州市| 来宾市| 务川| 翁牛特旗| 宁都县| 会东县| 广南县| 崇左市| 通海县| 桐柏县| 施秉县| 绿春县| 来凤县| 永定县| 柘城县| 海安县| 唐海县| 清徐县| 景德镇市| 汝州市| 太仆寺旗| 乌兰县|