From Jason Turner

[dcl.ref]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpttzvz8e0/{from.md → to.md} +39 -12
tmp/tmpttzvz8e0/{from.md → to.md} RENAMED
@@ -29,12 +29,11 @@ to `const int`”.
29
  — *end example*]
30
 
31
  [*Note 1*: A reference can be thought of as a name of an
32
  object. — *end note*]
33
 
34
- A declarator that specifies the type “reference to cv `void`” is
35
- ill-formed.
36
 
37
  A reference type that is declared using `&` is called an *lvalue
38
  reference*, and a reference type that is declared using `&&` is called
39
  an *rvalue reference*. Lvalue references and rvalue references are
40
  distinct types. Except where explicitly noted, they are semantically
@@ -95,29 +94,57 @@ There shall be no references to references, no arrays of references, and
95
  no pointers to references. The declaration of a reference shall contain
96
  an *initializer* [[dcl.init.ref]] except when the declaration contains
97
  an explicit `extern` specifier [[dcl.stc]], is a class member
98
  [[class.mem]] declaration within a class definition, or is the
99
  declaration of a parameter or a return type [[dcl.fct]]; see 
100
- [[basic.def]]. A reference shall be initialized to refer to a valid
101
- object or function.
102
-
103
- [*Note 2*: In particular, a null reference cannot exist in a
104
- well-defined program, because the only way to create such a reference
105
- would be to bind it to the object obtained by indirection through a
106
- null pointer, which causes undefined behavior. As described in 
107
- [[class.bit]], a reference cannot be bound directly to a
108
- bit-field. — *end note*]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  If a *typedef-name* [[dcl.typedef]], [[temp.param]] or a
111
  *decltype-specifier* [[dcl.type.decltype]] denotes a type `TR` that is a
112
  reference to a type `T`, an attempt to create the type “lvalue reference
113
  to cv `TR`” creates the type “lvalue reference to `T`”, while an attempt
114
  to create the type “rvalue reference to cv `TR`” creates the type `TR`.
115
 
116
  [*Note 3*: This rule is known as reference collapsing. — *end note*]
117
 
118
- [*Example 3*:
119
 
120
  ``` cpp
121
  int i;
122
  typedef int& LRI;
123
  typedef int&& RRI;
 
29
  — *end example*]
30
 
31
  [*Note 1*: A reference can be thought of as a name of an
32
  object. — *end note*]
33
 
34
+ Forming the type “reference to cv `void`” is ill-formed.
 
35
 
36
  A reference type that is declared using `&` is called an *lvalue
37
  reference*, and a reference type that is declared using `&&` is called
38
  an *rvalue reference*. Lvalue references and rvalue references are
39
  distinct types. Except where explicitly noted, they are semantically
 
94
  no pointers to references. The declaration of a reference shall contain
95
  an *initializer* [[dcl.init.ref]] except when the declaration contains
96
  an explicit `extern` specifier [[dcl.stc]], is a class member
97
  [[class.mem]] declaration within a class definition, or is the
98
  declaration of a parameter or a return type [[dcl.fct]]; see 
99
+ [[basic.def]].
100
+
101
+ Attempting to bind a reference to a function where the converted
102
+ initializer is a glvalue whose type is not call-compatible [[expr.call]]
103
+ with the type of the function’s definition results in undefined
104
+ behavior. Attempting to bind a reference to an object where the
105
+ converted initializer is a glvalue through which the object is not
106
+ type-accessible [[basic.lval]] results in undefined behavior.
107
+
108
+ [*Note 2*: The object designated by such a glvalue can be outside its
109
+ lifetime [[basic.life]]. Because a null pointer value or a pointer past
110
+ the end of an object does not point to an object, a reference in a
111
+ well-defined program cannot refer to such things; see 
112
+ [[expr.unary.op]]. As described in  [[class.bit]], a reference cannot be
113
+ bound directly to a bit-field. — *end note*]
114
+
115
+ The behavior of an evaluation of a reference
116
+ [[expr.prim.id]], [[expr.ref]] that does not happen after
117
+ [[intro.races]] the initialization of the reference is undefined.
118
+
119
+ [*Example 3*:
120
+
121
+ ``` cpp
122
+ int &f(int&);
123
+ int &g();
124
+ extern int &ir3;
125
+ int *ip = 0;
126
+ int &ir1 = *ip; // undefined behavior: null pointer
127
+ int &ir2 = f(ir3); // undefined behavior: ir3 not yet initialized
128
+ int &ir3 = g();
129
+ int &ir4 = f(ir4); // undefined behavior: ir4 used in its own initializer
130
+
131
+ char x alignas(int);
132
+ int &ir5 = *reinterpret_cast<int *>(&x); // undefined behavior: initializer refers to char object
133
+ ```
134
+
135
+ — *end example*]
136
 
137
  If a *typedef-name* [[dcl.typedef]], [[temp.param]] or a
138
  *decltype-specifier* [[dcl.type.decltype]] denotes a type `TR` that is a
139
  reference to a type `T`, an attempt to create the type “lvalue reference
140
  to cv `TR`” creates the type “lvalue reference to `T`”, while an attempt
141
  to create the type “rvalue reference to cv `TR`” creates the type `TR`.
142
 
143
  [*Note 3*: This rule is known as reference collapsing. — *end note*]
144
 
145
+ [*Example 4*:
146
 
147
  ``` cpp
148
  int i;
149
  typedef int& LRI;
150
  typedef int&& RRI;