extraction from The C++ Programming Language 4th. ed., Section 7.7 References, Bjarne Stroustrup

To reflect the lvalue/rvalue and const/non-const distinctions, there are three kinds of references:

  • lvalue references: to refer to objects whose value we want to change
  • const references: to refer to objects whose value we do not want to change (e.g., a constant)
  • rvalue references: to refer to objects whose value we do not need to preserve after we have used it (e.g., a temporary)

Collectively, they are called referencs. The first two are both called lvalue references.

 The obvious implementation of a reference is as a (constant) pointer that is dereferenced each time it is used. It doesn't do much harm to think about references that way, as long as one remembers that a reference isn't an object that can be manipulated the way a pointer is.

 In some cases, the compiler can optimize away a reference so that there is no object representing that reference at run time.

 Intialization of a reference is trivial when the initializer is an lvalue (an object whose address you can take). The initializer for a "plain" T& must be an lvaue of type T.

 The intializer for a const T& need not be an lvaue or even of type T. In such cases:

  1. First, implicit type conversion to T is applied if necessary.
  2. Then, the resulting value is placed in a temporary variable of type T.
  3. Finally, this temporary variable is used as the value of the initializer.

Consider:

  double &dr=1;      //error: lvalue needed

  const double& cdr{1};  //OK

The iterpretation of this last initialization might be:

  double temp = double{1};

  cosnt double &cdr {temp};

A temporary created to hold a reference initializer persists until the end of its reference's scope.

References to variables and references to constants are distinguished because introducing a temporary for a variable would have been highly error-prone; an assignment to the variable would become an assignment to the -- soon-to-disappear -- temporary. No such problem exists for references to constants, and references to constants are often important as function arguments.

C++ 之 const references的更多相关文章

  1. References & the Copy-Constructor

    1 There are certain rules when using references: (Page 451) A reference must be initialized when it ...

  2. const引用返回值

    一.引用 引用是别名 必须在定义引用时进行初始化.初始化是指明引用指向哪个对象的唯一方法. const 引用是指向 const 对象的引用: ; const int &refVal = iva ...

  3. 非const引用不能指向临时变量

    没找到具体原因,MSDN看到下面这句,VC是从2008才有这一限制的,感觉就是从语法上对临时变量增加了限定,因为一般说来修改一个临时变量是毫无意义的,通过增加限定,强调临时变量只读语义.虽然实际上修改 ...

  4. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  5. 总结c++ primer中的notes

    转载:http://blog.csdn.net/ace_fei/article/details/7386517 说明: C++ Primer, Fourth Edition (中英文)下载地址:htt ...

  6. C++: Why pass-by-value is generally more efficient than pass-by-reference for built-in (i.e., C-like) types

    A compiler vendor would typically implement a reference as a pointer. Pointers tend to be the same s ...

  7. 阅读Google的C++代码规范有感

    李开复曾在微博上说过,Google的C++代码规范是全球最好的一份C++代码规范,没有之一.最近花了点时间看了下这份代码规范,收获确实很大,在编程过程中一些乱七八糟的坏习惯也该改一改了.最新的英文版见 ...

  8. (转) Functions

    Functions Functions allow to structure programs in segments of code to perform individual tasks. In ...

  9. openssl 1.1.1 reference

    openssl 1.1.1 include/openssl aes.h: # define HEADER_AES_H aes.h: # define AES_ENCRYPT 1 aes.h: # de ...

随机推荐

  1. 每一个C#开发者必须知道的13件事情

    1.开发流程 程序的Bug与瑕疵往往出现于开发流程当中.只要对工具善加利用,就有助于在你发布程序之前便将问题发现,或避开这些问题. 标准化代码书写 标准化代码书写可以使代码更加易于维护,尤其是在代码由 ...

  2. [leetcode]算法题目 - Sudoku Solver

    最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...

  3. [BZOJ2152]聪聪可可(点分治)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2152 分析:裸的点分治,对于每课树,记录t[0],t[1],t[2]表示到当前根节点距 ...

  4. mybatis generator使用总结

    一.mybatis项目的体系结构 百度mybaits,可以进入mybatis的github:https://github.com/mybatis. mybatis是一个大大的体系,它不是孤立的,它可以 ...

  5. 显示当前用户所拥有的表&当前用户可以访问的所有表&数据库中的所有表&当前用户信息&当前用户所能管理的用户&数据库中所拥有的用户

    1)显示当前用户名称:show user或者show user; 2)显示当前用户信息(包括用户名之外还有其他信息):select * from user_users; 3)显示数据库中所含有的所有用 ...

  6. [转]VirtualBox – Error In supR3HardenedWinReSpawn 问题解决办法

    原文地址:http://chenpeng.info/html/3510 Genymotion 模拟器安装好虚拟机后,启动时报错: —————————VirtualBox – Error In supR ...

  7. js ajax请求

    var XMLHttpReq; function createXMLHttpRequest() { try { XMLHttpReq = new ActiveXObject("Msxml2. ...

  8. java-正则表达式过滤字符串中的html标签

    案例 import java.util.regex.Matcher; import java.util.regex.Pattern; /** * <p> * Title: HTML相关的正 ...

  9. TODO: http框架HttpManager的实现

    提供get, post, 同步, 异步方式访问网络

  10. 实在没想到系列——HashMap实现底层细节之keySet,values,entrySet的一个底层实现细节

    我在看HashMap源码的时候发现了一个没思考过的问题,在这之前可以说是完全没有思考过这个问题,在一开始对这个点有疑问的时候也没有想到居然有这么个语法细节存在,弄得我百思不得其解,直到自己动手做实验改 ...