- 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.
- Practical Data Analysis Cookbook
- Advanced Splunk
- ClickHouse性能之巔:從架構設計解讀性能之謎
- C++程序設計(第3版)
- Developing Mobile Web ArcGIS Applications
- 數(shù)據(jù)庫系統(tǒng)原理及MySQL應用教程
- AngularJS深度剖析與最佳實踐
- Learning Apache Mahout Classification
- Mastering Apache Maven 3
- Learning OpenCV 3 Computer Vision with Python(Second Edition)
- Babylon.js Essentials
- 微課學人工智能Python編程
- 零基礎學C語言(升級版)
- JQuery風暴:完美用戶體驗
- Clojure Polymorphism