From Jason Turner

[expr.static.cast]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmppmzk1twi/{from.md → to.md} +39 -28
tmp/tmppmzk1twi/{from.md → to.md} RENAMED
@@ -31,19 +31,19 @@ B &br = d;
31
  static_cast<D&>(br); // produces lvalue denoting the original d object
32
  ```
33
 
34
  — *end example*]
35
 
36
- An lvalue of type “*cv1* `T1` can be cast to type “rvalue reference to
37
- *cv2* `T2` if “*cv2* `T2`” is reference-compatible with “*cv1* `T1`
38
- [[dcl.init.ref]]. If the value is not a bit-field, the result refers to
39
- the object or the specified base class subobject thereof; otherwise, the
40
- lvalue-to-rvalue conversion [[conv.lval]] is applied to the bit-field
41
- and the resulting prvalue is used as the *expression* of the
42
- `static_cast` for the remainder of this subclause. If `T2` is an
43
- inaccessible [[class.access]] or ambiguous [[class.member.lookup]] base
44
- class of `T1`, a program that necessitates such a cast is ill-formed.
45
 
46
  An expression E can be explicitly converted to a type `T` if there is an
47
  implicit conversion sequence [[over.best.ics]] from E to `T`, if
48
  overload resolution for a direct-initialization [[dcl.init]] of an
49
  object or reference of type `T` from E would find at least one viable
@@ -72,13 +72,16 @@ direct-initialization defines the type of the expression as
72
  Otherwise, the `static_cast` shall perform one of the conversions listed
73
  below. No other conversion shall be performed explicitly using a
74
  `static_cast`.
75
 
76
  Any expression can be explicitly converted to type cv `void`, in which
77
- case it becomes a discarded-value expression [[expr.prop]].
78
 
79
- [*Note 3*: However, if the value is in a temporary object
 
 
 
80
  [[class.temporary]], the destructor for that object is not executed
81
  until the usual time, and the value of the object is preserved for the
82
  purpose of executing the destructor. — *end note*]
83
 
84
  The inverse of any standard conversion sequence [[conv]] not containing
@@ -116,39 +119,48 @@ explicitly converted to a floating-point type; the result is the same as
116
  that of converting from the original value to the floating-point type.
117
 
118
  A value of integral or enumeration type can be explicitly converted to a
119
  complete enumeration type. If the enumeration type has a fixed
120
  underlying type, the value is first converted to that type by integral
121
- conversion, if necessary, and then to the enumeration type. If the
122
- enumeration type does not have a fixed underlying type, the value is
123
- unchanged if the original value is within the range of the enumeration
124
- values [[dcl.enum]], and otherwise, the behavior is undefined. A value
125
- of floating-point type can also be explicitly converted to an
126
- enumeration type. The resulting value is the same as converting the
127
- original value to the underlying type of the enumeration [[conv.fpint]],
128
- and subsequently to the enumeration type.
 
 
 
 
 
 
 
 
 
129
 
130
  A prvalue of type “pointer to *cv1* `B`”, where `B` is a class type, can
131
  be converted to a prvalue of type “pointer to *cv2* `D`”, where `D` is a
132
  complete class derived [[class.derived]] from `B`, if *cv2* is the same
133
  cv-qualification as, or greater cv-qualification than, *cv1*. If `B` is
134
  a virtual base class of `D` or a base class of a virtual base class of
135
  `D`, or if no valid standard conversion from “pointer to `D`” to
136
  “pointer to `B`” exists [[conv.ptr]], the program is ill-formed. The
137
  null pointer value [[basic.compound]] is converted to the null pointer
138
  value of the destination type. If the prvalue of type “pointer to *cv1*
139
- `B`” points to a `B` that is actually a subobject of an object of type
140
- `D`, the resulting pointer points to the enclosing object of type `D`.
141
- Otherwise, the behavior is undefined.
142
 
143
  A prvalue of type “pointer to member of `D` of type *cv1* `T`” can be
144
  converted to a prvalue of type “pointer to member of `B` of type *cv2*
145
  `T`”, where `D` is a complete class type and `B` is a base class
146
  [[class.derived]] of `D`, if *cv2* is the same cv-qualification as, or
147
  greater cv-qualification than, *cv1*.
148
 
149
- [*Note 4*: Function types (including those used in
150
  pointer-to-member-function types) are never cv-qualified
151
  [[dcl.fct]]. — *end note*]
152
 
153
  If no valid standard conversion from “pointer to member of `B` of type
154
  `T`” to “pointer to member of `D` of type `T`” exists [[conv.mem]], the
@@ -157,11 +169,11 @@ converted to the null member pointer value of the destination type. If
157
  class `B` contains the original member, or is a base or derived class of
158
  the class containing the original member, the resulting pointer to
159
  member points to the original member. Otherwise, the behavior is
160
  undefined.
161
 
162
- [*Note 5*: Although class `B` need not contain the original member, the
163
  dynamic type of the object with which indirection through the pointer to
164
  member is performed must contain the original member; see 
165
  [[expr.mptr.oper]]. — *end note*]
166
 
167
  A prvalue of type “pointer to *cv1* `void`” can be converted to a
@@ -169,14 +181,13 @@ prvalue of type “pointer to *cv2* `T`”, where `T` is an object type and
169
  *cv2* is the same cv-qualification as, or greater cv-qualification than,
170
  *cv1*. If the original pointer value represents the address `A` of a
171
  byte in memory and `A` does not satisfy the alignment requirement of
172
  `T`, then the resulting pointer value is unspecified. Otherwise, if the
173
  original pointer value points to an object *a*, and there is an object
174
- *b* of type `T` (ignoring cv-qualification) that is
175
- pointer-interconvertible [[basic.compound]] with *a*, the result is a
176
- pointer to *b*. Otherwise, the pointer value is unchanged by the
177
- conversion.
178
 
179
  [*Example 3*:
180
 
181
  ``` cpp
182
  T* p1 = new T;
 
31
  static_cast<D&>(br); // produces lvalue denoting the original d object
32
  ```
33
 
34
  — *end example*]
35
 
36
+ An lvalue of type `T1` can be cast to type “rvalue reference to `T2`” if
37
+ `T2` is reference-compatible with `T1` [[dcl.init.ref]]. If the value is
38
+ not a bit-field, the result refers to the object or the specified base
39
+ class subobject thereof; otherwise, the lvalue-to-rvalue conversion
40
+ [[conv.lval]] is applied to the bit-field and the resulting prvalue is
41
+ used as the operand of the `static_cast` for the remainder of this
42
+ subclause. If `T2` is an inaccessible [[class.access]] or ambiguous
43
+ [[class.member.lookup]] base class of `T1`, a program that necessitates
44
+ such a cast is ill-formed.
45
 
46
  An expression E can be explicitly converted to a type `T` if there is an
47
  implicit conversion sequence [[over.best.ics]] from E to `T`, if
48
  overload resolution for a direct-initialization [[dcl.init]] of an
49
  object or reference of type `T` from E would find at least one viable
 
72
  Otherwise, the `static_cast` shall perform one of the conversions listed
73
  below. No other conversion shall be performed explicitly using a
74
  `static_cast`.
75
 
76
  Any expression can be explicitly converted to type cv `void`, in which
77
+ case the operand is a discarded-value expression [[expr.prop]].
78
 
79
+ [*Note 3*: Such a `static_cast` has no result as it is a prvalue of
80
+ type `void`; see  [[basic.lval]]. — *end note*]
81
+
82
+ [*Note 4*: However, if the value is in a temporary object
83
  [[class.temporary]], the destructor for that object is not executed
84
  until the usual time, and the value of the object is preserved for the
85
  purpose of executing the destructor. — *end note*]
86
 
87
  The inverse of any standard conversion sequence [[conv]] not containing
 
119
  that of converting from the original value to the floating-point type.
120
 
121
  A value of integral or enumeration type can be explicitly converted to a
122
  complete enumeration type. If the enumeration type has a fixed
123
  underlying type, the value is first converted to that type by integral
124
+ promotion [[conv.prom]] or integral conversion [[conv.integral]], if
125
+ necessary, and then to the enumeration type. If the enumeration type
126
+ does not have a fixed underlying type, the value is unchanged if the
127
+ original value is within the range of the enumeration values
128
+ [[dcl.enum]], and otherwise, the behavior is undefined. A value of
129
+ floating-point type can also be explicitly converted to an enumeration
130
+ type. The resulting value is the same as converting the original value
131
+ to the underlying type of the enumeration [[conv.fpint]], and
132
+ subsequently to the enumeration type.
133
+
134
+ A prvalue of floating-point type can be explicitly converted to any
135
+ other floating-point type. If the source value can be exactly
136
+ represented in the destination type, the result of the conversion has
137
+ that exact representation. If the source value is between two adjacent
138
+ destination values, the result of the conversion is an
139
+ *implementation-defined* choice of either of those values. Otherwise,
140
+ the behavior is undefined.
141
 
142
  A prvalue of type “pointer to *cv1* `B`”, where `B` is a class type, can
143
  be converted to a prvalue of type “pointer to *cv2* `D`”, where `D` is a
144
  complete class derived [[class.derived]] from `B`, if *cv2* is the same
145
  cv-qualification as, or greater cv-qualification than, *cv1*. If `B` is
146
  a virtual base class of `D` or a base class of a virtual base class of
147
  `D`, or if no valid standard conversion from “pointer to `D`” to
148
  “pointer to `B`” exists [[conv.ptr]], the program is ill-formed. The
149
  null pointer value [[basic.compound]] is converted to the null pointer
150
  value of the destination type. If the prvalue of type “pointer to *cv1*
151
+ `B`” points to a `B` that is actually a base class subobject of an
152
+ object of type `D`, the resulting pointer points to the enclosing object
153
+ of type `D`. Otherwise, the behavior is undefined.
154
 
155
  A prvalue of type “pointer to member of `D` of type *cv1* `T`” can be
156
  converted to a prvalue of type “pointer to member of `B` of type *cv2*
157
  `T`”, where `D` is a complete class type and `B` is a base class
158
  [[class.derived]] of `D`, if *cv2* is the same cv-qualification as, or
159
  greater cv-qualification than, *cv1*.
160
 
161
+ [*Note 5*: Function types (including those used in
162
  pointer-to-member-function types) are never cv-qualified
163
  [[dcl.fct]]. — *end note*]
164
 
165
  If no valid standard conversion from “pointer to member of `B` of type
166
  `T`” to “pointer to member of `D` of type `T`” exists [[conv.mem]], the
 
169
  class `B` contains the original member, or is a base or derived class of
170
  the class containing the original member, the resulting pointer to
171
  member points to the original member. Otherwise, the behavior is
172
  undefined.
173
 
174
+ [*Note 6*: Although class `B` need not contain the original member, the
175
  dynamic type of the object with which indirection through the pointer to
176
  member is performed must contain the original member; see 
177
  [[expr.mptr.oper]]. — *end note*]
178
 
179
  A prvalue of type “pointer to *cv1* `void`” can be converted to a
 
181
  *cv2* is the same cv-qualification as, or greater cv-qualification than,
182
  *cv1*. If the original pointer value represents the address `A` of a
183
  byte in memory and `A` does not satisfy the alignment requirement of
184
  `T`, then the resulting pointer value is unspecified. Otherwise, if the
185
  original pointer value points to an object *a*, and there is an object
186
+ *b* of type similar to `T` that is pointer-interconvertible
187
+ [[basic.compound]] with *a*, the result is a pointer to *b*. Otherwise,
188
+ the pointer value is unchanged by the conversion.
 
189
 
190
  [*Example 3*:
191
 
192
  ``` cpp
193
  T* p1 = new T;