- Expert C++
- Vardan Grigoryan Shunguang Wu
- 156字
- 2021-06-24 16:34:02
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.
- Python量化投資指南:基礎、數據與實戰
- Hands-On C++ Game Animation Programming
- 小程序,巧運營:微信小程序運營招式大全
- Hands-On Reinforcement Learning with Python
- Teaching with Google Classroom
- SQL Server從入門到精通(第3版)
- Mastering openFrameworks:Creative Coding Demystified
- C++從入門到精通(第5版)
- 基于ARM Cortex-M4F內核的MSP432 MCU開發實踐
- 細說Python編程:從入門到科學計算
- JQuery風暴:完美用戶體驗
- Joomla!Search Engine Optimization
- 編程的原則:改善代碼質量的101個方法
- 軟硬件綜合系統軟件需求建模及可靠性綜合試驗、分析、評價技術
- PhantomJS Cookbook