From Jason Turner

[conv]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpq0ulsb_7/{from.md → to.md} +167 -282
tmp/tmpq0ulsb_7/{from.md → to.md} RENAMED
@@ -1,19 +1,19 @@
1
- # Standard conversions <a id="conv">[[conv]]</a>
2
 
3
  Standard conversions are implicit conversions with built-in meaning.
4
- Clause  [[conv]] enumerates the full set of such conversions. A
5
- *standard conversion sequence* is a sequence of standard conversions in
6
- the following order:
7
 
8
  - Zero or one conversion from the following set: lvalue-to-rvalue
9
  conversion, array-to-pointer conversion, and function-to-pointer
10
  conversion.
11
  - Zero or one conversion from the following set: integral promotions,
12
  floating-point promotion, integral conversions, floating-point
13
  conversions, floating-integral conversions, pointer conversions,
14
- pointer to member conversions, and boolean conversions.
15
  - Zero or one function pointer conversion.
16
  - Zero or one qualification conversion.
17
 
18
  [*Note 1*: A standard conversion sequence can be empty, i.e., it can
19
  consist of no conversions. — *end note*]
@@ -25,78 +25,77 @@ necessary to convert it to a required destination type.
25
 
26
  Expressions with a given type will be implicitly converted to other
27
  types in several contexts:
28
 
29
  - When used as operands of operators. The operator’s requirements for
30
- its operands dictate the destination type (Clause  [[expr]]).
31
- - When used in the condition of an `if` statement or iteration
32
- statement ([[stmt.select]], [[stmt.iter]]). The destination type is
33
- `bool`.
34
- - When used in the expression of a `switch` statement. The destination
35
- type is integral ([[stmt.select]]).
36
  - When used as the source expression for an initialization (which
37
  includes use as an argument in a function call and use as the
38
  expression in a `return` statement). The type of the entity being
39
- initialized is (generally) the destination type. See  [[dcl.init]], 
40
  [[dcl.init.ref]].
41
 
42
  — *end note*]
43
 
44
- An expression `e` can be *implicitly converted* to a type `T` if and
45
- only if the declaration `T t=e;` is well-formed, for some invented
46
- temporary variable `t` ([[dcl.init]]).
47
 
48
  Certain language constructs require that an expression be converted to a
49
- Boolean value. An expression `e` appearing in such a context is said to
50
- be *contextually converted to `bool`* and is well-formed if and only if
51
- the declaration `bool t(e);` is well-formed, for some invented temporary
52
- variable `t` ([[dcl.init]]).
53
 
54
  Certain language constructs require conversion to a value having one of
55
- a specified set of types appropriate to the construct. An expression `e`
56
- of class type `E` appearing in such a context is said to be
57
  *contextually implicitly converted* to a specified type `T` and is
58
- well-formed if and only if `e` can be implicitly converted to a type `T`
59
- that is determined as follows: `E` is searched for non-explicit
60
- conversion functions whose return type is *cv* `T` or reference to *cv*
61
- `T` such that `T` is allowed by the context. There shall be exactly one
62
- such `T`.
63
 
64
  The effect of any implicit conversion is the same as performing the
65
  corresponding declaration and initialization and then using the
66
  temporary variable as the result of the conversion. The result is an
67
  lvalue if `T` is an lvalue reference type or an rvalue reference to
68
- function type ([[dcl.ref]]), an xvalue if `T` is an rvalue reference to
69
- object type, and a prvalue otherwise. The expression `e` is used as a
70
  glvalue if and only if the initialization uses it as a glvalue.
71
 
72
  [*Note 3*: For class types, user-defined conversions are considered as
73
- well; see  [[class.conv]]. In general, an implicit conversion sequence (
74
- [[over.best.ics]]) consists of a standard conversion sequence followed
75
- by a user-defined conversion followed by another standard conversion
76
  sequence. — *end note*]
77
 
78
  [*Note 4*: There are some contexts where certain conversions are
79
  suppressed. For example, the lvalue-to-rvalue conversion is not done on
80
  the operand of the unary `&` operator. Specific exceptions are given in
81
  the descriptions of those operators and contexts. — *end note*]
82
 
83
- ## Lvalue-to-rvalue conversion <a id="conv.lval">[[conv.lval]]</a>
84
 
85
- A glvalue ([[basic.lval]]) of a non-function, non-array type `T` can be
86
- converted to a prvalue.[^1] If `T` is an incomplete type, a program that
87
  necessitates this conversion is ill-formed. If `T` is a non-class type,
88
  the type of the prvalue is the cv-unqualified version of `T`. Otherwise,
89
- the type of the prvalue is `T`. [^2]
90
 
91
- When an lvalue-to-rvalue conversion is applied to an expression `e`, and
92
  either
93
 
94
- - `e` is not potentially evaluated, or
95
- - the evaluation of `e` results in the evaluation of a member `ex` of
96
- the set of potential results of `e`, and `ex` names a variable `x`
97
- that is not odr-used by `ex` ([[basic.def.odr]]),
98
 
99
  the value contained in the referenced object is not accessed.
100
 
101
  [*Example 1*:
102
 
@@ -106,54 +105,52 @@ auto f() {
106
  S x { 1 };
107
  constexpr S y { 2 };
108
  return [&](bool b) { return (b ? y : x).n; };
109
  }
110
  auto g = f();
111
- int m = g(false); // undefined behavior due to access of x.n outside its lifetime
112
  int n = g(true); // OK, does not access y.n
113
  ```
114
 
115
  — *end example*]
116
 
117
  The result of the conversion is determined according to the following
118
  rules:
119
 
120
- - If `T` is cv `std::nullptr_t`, the result is a null pointer constant (
121
- [[conv.ptr]]). \[*Note 1*: Since no value is fetched from memory,
122
- there is no side effect for a volatile access ([[intro.execution]]),
123
- and an inactive member of a union ([[class.union]]) may be
124
- accessed. — *end note*]
125
  - Otherwise, if `T` has a class type, the conversion copy-initializes
126
  the result object from the glvalue.
127
  - Otherwise, if the object to which the glvalue refers contains an
128
  invalid pointer value ([[basic.stc.dynamic.deallocation]],
129
  [[basic.stc.dynamic.safety]]), the behavior is
130
  *implementation-defined*.
131
- - Otherwise, the value contained in the object indicated by the glvalue
132
- is the prvalue result.
 
133
 
134
  [*Note 2*: See also  [[basic.lval]]. — *end note*]
135
 
136
- ## Array-to-pointer conversion <a id="conv.array">[[conv.array]]</a>
137
 
138
  An lvalue or rvalue of type “array of `N` `T`” or “array of unknown
139
  bound of `T`” can be converted to a prvalue of type “pointer to `T`”.
140
- The temporary materialization conversion ([[conv.rval]]) is applied.
141
- The result is a pointer to the first element of the array.
142
 
143
- ## Function-to-pointer conversion <a id="conv.func">[[conv.func]]</a>
144
 
145
  An lvalue of function type `T` can be converted to a prvalue of type
146
- “pointer to `T`”. The result is a pointer to the function.[^3]
147
 
148
- [*Note 1*: See  [[over.over]] for additional rules for the case where
149
- the function is overloaded. — *end note*]
150
-
151
- ## Temporary materialization conversion <a id="conv.rval">[[conv.rval]]</a>
152
 
153
  A prvalue of type `T` can be converted to an xvalue of type `T`. This
154
- conversion initializes a temporary object ([[class.temporary]]) of type
155
  `T` from the prvalue by evaluating the prvalue with the temporary object
156
  as its result object, and produces an xvalue denoting the temporary
157
  object. `T` shall be a complete type.
158
 
159
  [*Note 1*: If `T` is a class type (or array thereof), it must have an
@@ -167,49 +164,54 @@ struct X { int n; };
167
  int k = X().n; // OK, X() prvalue is converted to xvalue
168
  ```
169
 
170
  — *end example*]
171
 
172
- ## Qualification conversions <a id="conv.qual">[[conv.qual]]</a>
173
 
174
  A *cv-decomposition* of a type `T` is a sequence of cvᵢ and Pᵢ such that
175
  `T` is
176
 
177
- where each cvᵢ is a set of cv-qualifiers ([[basic.type.qualifier]]),
178
- and each Pᵢ is “pointer to” ([[dcl.ptr]]), “pointer to member of class
179
- Cᵢ of type” ([[dcl.mptr]]), “array of Nᵢ”, or “array of unknown bound
180
- of” ([[dcl.array]]). If Pᵢ designates an array, the cv-qualifiers cvᵢ₊₁
181
- on the element type are also taken as the cv-qualifiers cvᵢ of the
182
- array.
183
 
184
- [*Example 1*: The type denoted by the *type-id* `const int **` has two
185
- cv-decompositions, taking `U` as “`int`” and as “pointer to
 
186
  `const int`”. — *end example*]
187
 
188
  The n-tuple of cv-qualifiers after the first one in the longest
189
  cv-decomposition of `T`, that is, cv₁, cv₂, …, cvₙ, is called the
190
  *cv-qualification signature* of `T`.
191
 
192
- Two types `T₁` and `T₂` are *similar* if they have cv-decompositions
193
- with the same n such that corresponding Pᵢ components are the same and
194
- the types denoted by `U` are the same.
 
195
 
196
- A prvalue expression of type `T₁` can be converted to type `T₂` if the
197
- following conditions are satisfied, where cvᵢʲ denotes the cv-qualifiers
198
- in the cv-qualification signature of `Tⱼ`: [^4]
199
 
200
- - `T₁` and `T₂` are similar.
201
- - For every i > 0, if `const` is in cvᵢ¹ then `const` is in cvᵢ², and
202
- similarly for `volatile`.
203
- - If the cvᵢ¹ and cvᵢ² are different, then `const` is in every cv² for
204
- 0 < k < i.
 
 
 
 
 
205
 
206
  [*Note 1*:
207
 
208
  If a program could assign a pointer of type `T**` to a pointer of type
209
  `const` `T**` (that is, if line \#1 below were allowed), a program could
210
- inadvertently modify a `const` object (as it is done on line \#2). For
211
  example,
212
 
213
  ``` cpp
214
  int main() {
215
  const char c = 'c';
@@ -220,59 +222,62 @@ int main() {
220
  }
221
  ```
222
 
223
  — *end note*]
224
 
225
- [*Note 2*: A prvalue of type “pointer to *cv1* `T` can be converted to
 
 
 
 
226
  a prvalue of type “pointer to *cv2* `T`” if “*cv2* `T`” is more
227
  cv-qualified than “*cv1* `T`”. A prvalue of type “pointer to member of
228
  `X` of type *cv1* `T`” can be converted to a prvalue of type “pointer to
229
  member of `X` of type *cv2* `T`” if “*cv2* `T`” is more cv-qualified
230
  than “*cv1* `T`”. — *end note*]
231
 
232
- [*Note 3*: Function types (including those used in pointer to member
233
- function types) are never cv-qualified ([[dcl.fct]]). — *end note*]
 
234
 
235
- ## Integral promotions <a id="conv.prom">[[conv.prom]]</a>
236
 
237
  A prvalue of an integer type other than `bool`, `char16_t`, `char32_t`,
238
- or `wchar_t` whose integer conversion rank ([[conv.rank]]) is less than
239
  the rank of `int` can be converted to a prvalue of type `int` if `int`
240
  can represent all the values of the source type; otherwise, the source
241
  prvalue can be converted to a prvalue of type `unsigned int`.
242
 
243
- A prvalue of type `char16_t`, `char32_t`, or `wchar_t` (
244
- [[basic.fundamental]]) can be converted to a prvalue of the first of the
245
  following types that can represent all the values of its underlying
246
  type: `int`, `unsigned int`, `long int`, `unsigned long int`,
247
  `long long int`, or `unsigned long long int`. If none of the types in
248
  that list can represent all the values of its underlying type, a prvalue
249
  of type `char16_t`, `char32_t`, or `wchar_t` can be converted to a
250
  prvalue of its underlying type.
251
 
252
  A prvalue of an unscoped enumeration type whose underlying type is not
253
- fixed ([[dcl.enum]]) can be converted to a prvalue of the first of the
254
- following types that can represent all the values of the enumeration
255
- (i.e., the values in the range bₘin to bₘax as described in 
256
- [[dcl.enum]]): `int`, `unsigned int`, `long int`, `unsigned long int`,
257
- `long long int`, or `unsigned long long int`. If none of the types in
258
- that list can represent all the values of the enumeration, a prvalue of
259
- an unscoped enumeration type can be converted to a prvalue of the
260
- extended integer type with lowest integer conversion rank (
261
- [[conv.rank]]) greater than the rank of `long long` in which all the
262
- values of the enumeration can be represented. If there are two such
263
- extended types, the signed one is chosen.
264
 
265
- A prvalue of an unscoped enumeration type whose underlying type is
266
- fixed ([[dcl.enum]]) can be converted to a prvalue of its underlying
267
- type. Moreover, if integral promotion can be applied to its underlying
268
- type, a prvalue of an unscoped enumeration type whose underlying type is
269
- fixed can also be converted to a prvalue of the promoted underlying
270
- type.
271
 
272
- A prvalue for an integral bit-field ([[class.bit]]) can be converted to
273
- a prvalue of type `int` if `int` can represent all the values of the
274
  bit-field; otherwise, it can be converted to `unsigned int` if
275
  `unsigned int` can represent all the values of the bit-field. If the
276
  bit-field is larger yet, no integral promotion applies to it. If the
277
  bit-field has an enumerated type, it is treated as any other value of
278
  that type for promotion purposes.
@@ -280,43 +285,35 @@ that type for promotion purposes.
280
  A prvalue of type `bool` can be converted to a prvalue of type `int`,
281
  with `false` becoming zero and `true` becoming one.
282
 
283
  These conversions are called *integral promotions*.
284
 
285
- ## Floating-point promotion <a id="conv.fpprom">[[conv.fpprom]]</a>
286
 
287
  A prvalue of type `float` can be converted to a prvalue of type
288
  `double`. The value is unchanged.
289
 
290
  This conversion is called *floating-point promotion*.
291
 
292
- ## Integral conversions <a id="conv.integral">[[conv.integral]]</a>
293
 
294
  A prvalue of an integer type can be converted to a prvalue of another
295
  integer type. A prvalue of an unscoped enumeration type can be converted
296
  to a prvalue of an integer type.
297
 
298
- If the destination type is unsigned, the resulting value is the least
299
- unsigned integer congruent to the source integer (modulo 2ⁿ where n is
300
- the number of bits used to represent the unsigned type).
301
-
302
- [*Note 1*: In a two’s complement representation, this conversion is
303
- conceptual and there is no change in the bit pattern (if there is no
304
- truncation). — *end note*]
305
-
306
- If the destination type is signed, the value is unchanged if it can be
307
- represented in the destination type; otherwise, the value is
308
- *implementation-defined*.
309
-
310
  If the destination type is `bool`, see  [[conv.bool]]. If the source
311
  type is `bool`, the value `false` is converted to zero and the value
312
  `true` is converted to one.
313
 
 
 
 
 
314
  The conversions allowed as integral promotions are excluded from the set
315
  of integral conversions.
316
 
317
- ## Floating-point conversions <a id="conv.double">[[conv.double]]</a>
318
 
319
  A prvalue of floating-point type can be converted to a prvalue of
320
  another floating-point type. If the source value can be exactly
321
  represented in the destination type, the result of the conversion is
322
  that exact representation. If the source value is between two adjacent
@@ -325,11 +322,11 @@ destination values, the result of the conversion is an
325
  the behavior is undefined.
326
 
327
  The conversions allowed as floating-point promotions are excluded from
328
  the set of floating-point conversions.
329
 
330
- ## Floating-integral conversions <a id="conv.fpint">[[conv.fpint]]</a>
331
 
332
  A prvalue of a floating-point type can be converted to a prvalue of an
333
  integer type. The conversion truncates; that is, the fractional part is
334
  discarded. The behavior is undefined if the truncated value cannot be
335
  represented in the destination type.
@@ -343,83 +340,83 @@ possible. If the value being converted is in the range of values that
343
  can be represented but the value cannot be represented exactly, it is an
344
  *implementation-defined* choice of either the next lower or higher
345
  representable value.
346
 
347
  [*Note 2*: Loss of precision occurs if the integral value cannot be
348
- represented exactly as a value of the floating type. — *end note*]
 
349
 
350
  If the value being converted is outside the range of values that can be
351
  represented, the behavior is undefined. If the source type is `bool`,
352
  the value `false` is converted to zero and the value `true` is converted
353
  to one.
354
 
355
- ## Pointer conversions <a id="conv.ptr">[[conv.ptr]]</a>
356
 
357
- A *null pointer constant* is an integer literal ([[lex.icon]]) with
358
- value zero or a prvalue of type `std::nullptr_t`. A null pointer
359
- constant can be converted to a pointer type; the result is the *null
360
- pointer value* of that type and is distinguishable from every other
361
  value of object pointer or function pointer type. Such a conversion is
362
  called a *null pointer conversion*. Two null pointer values of the same
363
  type shall compare equal. The conversion of a null pointer constant to a
364
  pointer to cv-qualified type is a single conversion, and not the
365
- sequence of a pointer conversion followed by a qualification
366
- conversion ([[conv.qual]]). A null pointer constant of integral type
367
- can be converted to a prvalue of type `std::nullptr_t`.
368
 
369
  [*Note 1*: The resulting prvalue is not a null pointer
370
  value. — *end note*]
371
 
372
- A prvalue of type “pointer to *cv* `T`”, where `T` is an object type,
373
- can be converted to a prvalue of type “pointer to *cv* `void`”. The
374
- pointer value ([[basic.compound]]) is unchanged by this conversion.
375
 
376
- A prvalue of type “pointer to *cv* `D`”, where `D` is a class type, can
377
- be converted to a prvalue of type “pointer to *cv* `B`”, where `B` is a
378
- base class (Clause  [[class.derived]]) of `D`. If `B` is an inaccessible
379
- (Clause  [[class.access]]) or ambiguous ([[class.member.lookup]]) base
380
- class of `D`, a program that necessitates this conversion is ill-formed.
381
- The result of the conversion is a pointer to the base class subobject of
382
- the derived class object. The null pointer value is converted to the
383
- null pointer value of the destination type.
384
 
385
- ## Pointer to member conversions <a id="conv.mem">[[conv.mem]]</a>
386
 
387
- A null pointer constant ([[conv.ptr]]) can be converted to a pointer to
388
- member type; the result is the *null member pointer value* of that type
389
- and is distinguishable from any pointer to member not created from a
390
- null pointer constant. Such a conversion is called a *null member
391
  pointer conversion*. Two null member pointer values of the same type
392
  shall compare equal. The conversion of a null pointer constant to a
393
  pointer to member of cv-qualified type is a single conversion, and not
394
- the sequence of a pointer to member conversion followed by a
395
- qualification conversion ([[conv.qual]]).
396
 
397
- A prvalue of type “pointer to member of `B` of type *cv* `T`”, where `B`
398
  is a class type, can be converted to a prvalue of type “pointer to
399
- member of `D` of type *cv* `T`”, where `D` is a derived class (Clause 
400
- [[class.derived]]) of `B`. If `B` is an inaccessible (Clause 
401
- [[class.access]]), ambiguous ([[class.member.lookup]]), or virtual (
402
- [[class.mi]]) base class of `D`, or a base class of a virtual base class
403
- of `D`, a program that necessitates this conversion is ill-formed. The
404
- result of the conversion refers to the same member as the pointer to
405
- member before the conversion took place, but it refers to the base class
406
- member as if it were a member of the derived class. The result refers to
407
- the member in `D`’s instance of `B`. Since the result has type “pointer
408
- to member of `D` of type *cv* `T`”, indirection through it with a `D`
409
- object is valid. The result is the same as if indirecting through the
410
- pointer to member of `B` with the `B` subobject of `D`. The null member
411
- pointer value is converted to the null member pointer value of the
412
- destination type.[^5]
413
 
414
- ## Function pointer conversions <a id="conv.fctptr">[[conv.fctptr]]</a>
415
 
416
  A prvalue of type “pointer to `noexcept` function” can be converted to a
417
  prvalue of type “pointer to function”. The result is a pointer to the
418
  function. A prvalue of type “pointer to member of type `noexcept`
419
  function” can be converted to a prvalue of type “pointer to member of
420
- type function”. The result points to the member function.
421
 
422
  [*Example 1*:
423
 
424
  ``` cpp
425
  void (*p)();
@@ -429,124 +426,12 @@ type function”. The result points to the member function.
429
  void (*q)() noexcept = S(); // error: cannot convert to pointer to noexcept function
430
  ```
431
 
432
  — *end example*]
433
 
434
- ## Boolean conversions <a id="conv.bool">[[conv.bool]]</a>
435
 
436
- A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to
437
- member type can be converted to a prvalue of type `bool`. A zero value,
438
- null pointer value, or null member pointer value is converted to
439
- `false`; any other value is converted to `true`. For
440
- direct-initialization ([[dcl.init]]), a prvalue of type
441
- `std::nullptr_t` can be converted to a prvalue of type `bool`; the
442
- resulting value is `false`.
443
 
444
- ## Integer conversion rank <a id="conv.rank">[[conv.rank]]</a>
445
-
446
- Every integer type has an *integer conversion rank* defined as follows:
447
-
448
- - No two signed integer types other than `char` and `signed
449
- char` (if `char` is signed) shall have the same rank, even if they
450
- have the same representation.
451
- - The rank of a signed integer type shall be greater than the rank of
452
- any signed integer type with a smaller size.
453
- - The rank of `long long int` shall be greater than the rank of
454
- `long int`, which shall be greater than the rank of `int`, which shall
455
- be greater than the rank of `short int`, which shall be greater than
456
- the rank of `signed char`.
457
- - The rank of any unsigned integer type shall equal the rank of the
458
- corresponding signed integer type.
459
- - The rank of any standard integer type shall be greater than the rank
460
- of any extended integer type with the same size.
461
- - The rank of `char` shall equal the rank of `signed char` and
462
- `unsigned char`.
463
- - The rank of `bool` shall be less than the rank of all other standard
464
- integer types.
465
- - The ranks of `char16_t`, `char32_t`, and `wchar_t` shall equal the
466
- ranks of their underlying types ([[basic.fundamental]]).
467
- - The rank of any extended signed integer type relative to another
468
- extended signed integer type with the same size is
469
- *implementation-defined*, but still subject to the other rules for
470
- determining the integer conversion rank.
471
- - For all integer types `T1`, `T2`, and `T3`, if `T1` has greater rank
472
- than `T2` and `T2` has greater rank than `T3`, then `T1` shall have
473
- greater rank than `T3`.
474
-
475
- [*Note 1*: The integer conversion rank is used in the definition of the
476
- integral promotions ([[conv.prom]]) and the usual arithmetic
477
- conversions (Clause  [[expr]]). — *end note*]
478
-
479
- <!-- Link reference definitions -->
480
- [basic.compound]: basic.md#basic.compound
481
- [basic.def.odr]: basic.md#basic.def.odr
482
- [basic.fundamental]: basic.md#basic.fundamental
483
- [basic.lval]: basic.md#basic.lval
484
- [basic.stc.dynamic.deallocation]: basic.md#basic.stc.dynamic.deallocation
485
- [basic.stc.dynamic.safety]: basic.md#basic.stc.dynamic.safety
486
- [basic.type.qualifier]: basic.md#basic.type.qualifier
487
- [class.access]: class.md#class.access
488
- [class.bit]: class.md#class.bit
489
- [class.conv]: special.md#class.conv
490
- [class.derived]: class.md#class.derived
491
- [class.dtor]: special.md#class.dtor
492
- [class.member.lookup]: class.md#class.member.lookup
493
- [class.mi]: class.md#class.mi
494
- [class.temporary]: special.md#class.temporary
495
- [class.union]: class.md#class.union
496
- [conv]: #conv
497
- [conv.array]: #conv.array
498
- [conv.bool]: #conv.bool
499
- [conv.double]: #conv.double
500
- [conv.fctptr]: #conv.fctptr
501
- [conv.fpint]: #conv.fpint
502
- [conv.fpprom]: #conv.fpprom
503
- [conv.func]: #conv.func
504
- [conv.integral]: #conv.integral
505
- [conv.lval]: #conv.lval
506
- [conv.mem]: #conv.mem
507
- [conv.prom]: #conv.prom
508
- [conv.ptr]: #conv.ptr
509
- [conv.qual]: #conv.qual
510
- [conv.rank]: #conv.rank
511
- [conv.rval]: #conv.rval
512
- [dcl.array]: dcl.md#dcl.array
513
- [dcl.enum]: dcl.md#dcl.enum
514
- [dcl.fct]: dcl.md#dcl.fct
515
- [dcl.init]: dcl.md#dcl.init
516
- [dcl.init.ref]: dcl.md#dcl.init.ref
517
- [dcl.mptr]: dcl.md#dcl.mptr
518
- [dcl.ptr]: dcl.md#dcl.ptr
519
- [dcl.ref]: dcl.md#dcl.ref
520
- [expr]: expr.md#expr
521
- [intro.execution]: intro.md#intro.execution
522
- [lex.icon]: lex.md#lex.icon
523
- [over.best.ics]: over.md#over.best.ics
524
- [over.over]: over.md#over.over
525
- [stmt.iter]: stmt.md#stmt.iter
526
- [stmt.select]: stmt.md#stmt.select
527
-
528
- [^1]: For historical reasons, this conversion is called the
529
- “lvalue-to-rvalue” conversion, even though that name does not
530
- accurately reflect the taxonomy of expressions described in 
531
- [[basic.lval]].
532
-
533
- [^2]: In C++class and array prvalues can have cv-qualified types. This
534
- differs from ISO C, in which non-lvalues never have cv-qualified
535
- types.
536
-
537
- [^3]: This conversion never applies to non-static member functions
538
- because an lvalue that refers to a non-static member function cannot
539
- be obtained.
540
-
541
- [^4]: These rules ensure that const-safety is preserved by the
542
- conversion.
543
-
544
- [^5]: The rule for conversion of pointers to members (from pointer to
545
- member of base to pointer to member of derived) appears inverted
546
- compared to the rule for pointers to objects (from pointer to
547
- derived to pointer to base) ([[conv.ptr]], Clause 
548
- [[class.derived]]). This inversion is necessary to ensure type
549
- safety. Note that a pointer to member is not an object pointer or a
550
- function pointer and the rules for conversions of such pointers do
551
- not apply to pointers to members. In particular, a pointer to member
552
- cannot be converted to a `void*`.
 
1
+ ## Standard conversions <a id="conv">[[conv]]</a>
2
 
3
  Standard conversions are implicit conversions with built-in meaning.
4
+ [[conv]] enumerates the full set of such conversions. A *standard
5
+ conversion sequence* is a sequence of standard conversions in the
6
+ following order:
7
 
8
  - Zero or one conversion from the following set: lvalue-to-rvalue
9
  conversion, array-to-pointer conversion, and function-to-pointer
10
  conversion.
11
  - Zero or one conversion from the following set: integral promotions,
12
  floating-point promotion, integral conversions, floating-point
13
  conversions, floating-integral conversions, pointer conversions,
14
+ pointer-to-member conversions, and boolean conversions.
15
  - Zero or one function pointer conversion.
16
  - Zero or one qualification conversion.
17
 
18
  [*Note 1*: A standard conversion sequence can be empty, i.e., it can
19
  consist of no conversions. — *end note*]
 
25
 
26
  Expressions with a given type will be implicitly converted to other
27
  types in several contexts:
28
 
29
  - When used as operands of operators. The operator’s requirements for
30
+ its operands dictate the destination type [[expr.compound]].
31
+ - When used in the condition of an `if` statement [[stmt.if]] or
32
+ iteration statement [[stmt.iter]]. The destination type is `bool`.
33
+ - When used in the expression of a `switch` statement [[stmt.switch]].
34
+ The destination type is integral.
 
35
  - When used as the source expression for an initialization (which
36
  includes use as an argument in a function call and use as the
37
  expression in a `return` statement). The type of the entity being
38
+ initialized is (generally) the destination type. See  [[dcl.init]],
39
  [[dcl.init.ref]].
40
 
41
  — *end note*]
42
 
43
+ An expression E can be *implicitly converted* to a type `T` if and only
44
+ if the declaration `T t=E;` is well-formed, for some invented temporary
45
+ variable `t` [[dcl.init]].
46
 
47
  Certain language constructs require that an expression be converted to a
48
+ Boolean value. An expression E appearing in such a context is said to be
49
+ *contextually converted to `bool`* and is well-formed if and only if the
50
+ declaration `bool t(E);` is well-formed, for some invented temporary
51
+ variable `t` [[dcl.init]].
52
 
53
  Certain language constructs require conversion to a value having one of
54
+ a specified set of types appropriate to the construct. An expression E
55
+ of class type `C` appearing in such a context is said to be
56
  *contextually implicitly converted* to a specified type `T` and is
57
+ well-formed if and only if E can be implicitly converted to a type `T`
58
+ that is determined as follows: `C` is searched for non-explicit
59
+ conversion functions whose return type is cv `T` or reference to cv `T`
60
+ such that `T` is allowed by the context. There shall be exactly one such
61
+ `T`.
62
 
63
  The effect of any implicit conversion is the same as performing the
64
  corresponding declaration and initialization and then using the
65
  temporary variable as the result of the conversion. The result is an
66
  lvalue if `T` is an lvalue reference type or an rvalue reference to
67
+ function type [[dcl.ref]], an xvalue if `T` is an rvalue reference to
68
+ object type, and a prvalue otherwise. The expression E is used as a
69
  glvalue if and only if the initialization uses it as a glvalue.
70
 
71
  [*Note 3*: For class types, user-defined conversions are considered as
72
+ well; see  [[class.conv]]. In general, an implicit conversion sequence
73
+ [[over.best.ics]] consists of a standard conversion sequence followed by
74
+ a user-defined conversion followed by another standard conversion
75
  sequence. — *end note*]
76
 
77
  [*Note 4*: There are some contexts where certain conversions are
78
  suppressed. For example, the lvalue-to-rvalue conversion is not done on
79
  the operand of the unary `&` operator. Specific exceptions are given in
80
  the descriptions of those operators and contexts. — *end note*]
81
 
82
+ ### Lvalue-to-rvalue conversion <a id="conv.lval">[[conv.lval]]</a>
83
 
84
+ A glvalue [[basic.lval]] of a non-function, non-array type `T` can be
85
+ converted to a prvalue.[^5] If `T` is an incomplete type, a program that
86
  necessitates this conversion is ill-formed. If `T` is a non-class type,
87
  the type of the prvalue is the cv-unqualified version of `T`. Otherwise,
88
+ the type of the prvalue is `T`. [^6]
89
 
90
+ When an lvalue-to-rvalue conversion is applied to an expression E, and
91
  either
92
 
93
+ - E is not potentially evaluated, or
94
+ - the evaluation of E results in the evaluation of a member E_`x` of the
95
+ set of potential results of E, and E_`x` names a variable `x` that is
96
+ not odr-used by E_`x` [[basic.def.odr]],
97
 
98
  the value contained in the referenced object is not accessed.
99
 
100
  [*Example 1*:
101
 
 
105
  S x { 1 };
106
  constexpr S y { 2 };
107
  return [&](bool b) { return (b ? y : x).n; };
108
  }
109
  auto g = f();
110
+ int m = g(false); // undefined behavior: access of x.n outside its lifetime
111
  int n = g(true); // OK, does not access y.n
112
  ```
113
 
114
  — *end example*]
115
 
116
  The result of the conversion is determined according to the following
117
  rules:
118
 
119
+ - If `T` is cv `std::nullptr_t`, the result is a null pointer constant
120
+ [[conv.ptr]]. \[*Note 1*: Since the conversion does not access the
121
+ object to which the glvalue refers, there is no side effect even if
122
+ `T` is volatile-qualified [[intro.execution]], and the glvalue can
123
+ refer to an inactive member of a union [[class.union]]. — *end note*]
124
  - Otherwise, if `T` has a class type, the conversion copy-initializes
125
  the result object from the glvalue.
126
  - Otherwise, if the object to which the glvalue refers contains an
127
  invalid pointer value ([[basic.stc.dynamic.deallocation]],
128
  [[basic.stc.dynamic.safety]]), the behavior is
129
  *implementation-defined*.
130
+ - Otherwise, the object indicated by the glvalue is read
131
+ [[defns.access]], and the value contained in the object is the prvalue
132
+ result.
133
 
134
  [*Note 2*: See also  [[basic.lval]]. — *end note*]
135
 
136
+ ### Array-to-pointer conversion <a id="conv.array">[[conv.array]]</a>
137
 
138
  An lvalue or rvalue of type “array of `N` `T`” or “array of unknown
139
  bound of `T`” can be converted to a prvalue of type “pointer to `T`”.
140
+ The temporary materialization conversion [[conv.rval]] is applied. The
141
+ result is a pointer to the first element of the array.
142
 
143
+ ### Function-to-pointer conversion <a id="conv.func">[[conv.func]]</a>
144
 
145
  An lvalue of function type `T` can be converted to a prvalue of type
146
+ “pointer to `T`”. The result is a pointer to the function.[^7]
147
 
148
+ ### Temporary materialization conversion <a id="conv.rval">[[conv.rval]]</a>
 
 
 
149
 
150
  A prvalue of type `T` can be converted to an xvalue of type `T`. This
151
+ conversion initializes a temporary object [[class.temporary]] of type
152
  `T` from the prvalue by evaluating the prvalue with the temporary object
153
  as its result object, and produces an xvalue denoting the temporary
154
  object. `T` shall be a complete type.
155
 
156
  [*Note 1*: If `T` is a class type (or array thereof), it must have an
 
164
  int k = X().n; // OK, X() prvalue is converted to xvalue
165
  ```
166
 
167
  — *end example*]
168
 
169
+ ### Qualification conversions <a id="conv.qual">[[conv.qual]]</a>
170
 
171
  A *cv-decomposition* of a type `T` is a sequence of cvᵢ and Pᵢ such that
172
  `T` is
173
 
174
+ where each cvᵢ is a set of cv-qualifiers [[basic.type.qualifier]], and
175
+ each Pᵢ is “pointer to” [[dcl.ptr]], “pointer to member of class Cᵢ of
176
+ type” [[dcl.mptr]], “array of Nᵢ”, or “array of unknown bound of”
177
+ [[dcl.array]]. If Pᵢ designates an array, the cv-qualifiers cvᵢ₊₁ on the
178
+ element type are also taken as the cv-qualifiers cvᵢ of the array.
 
179
 
180
+ [*Example 1*: The type denoted by the *type-id* `const int **` has
181
+ three cv-decompositions, taking `U` as “`int`”, as “pointer to
182
+ `const int`”, and as “pointer to pointer to
183
  `const int`”. — *end example*]
184
 
185
  The n-tuple of cv-qualifiers after the first one in the longest
186
  cv-decomposition of `T`, that is, cv₁, cv₂, …, cvₙ, is called the
187
  *cv-qualification signature* of `T`.
188
 
189
+ Two types `T1` and `T2` are *similar* if they have cv-decompositions
190
+ with the same n such that corresponding Pᵢ components are either the
191
+ same or one is “array of Nᵢ” and the other is “array of unknown bound
192
+ of”, and the types denoted by `U` are the same.
193
 
194
+ The *cv-combined type* of two types `T1` and `T2` is the type `T3`
195
+ similar to `T1` whose cv-decomposition is such that:
 
196
 
197
+ - for every i > 0, cv³ᵢ is the union of cv¹ᵢ and cv²ᵢ;
198
+ - if either P¹ᵢ or P²ᵢ is “array of unknown bound of”, P³ᵢ is “array of
199
+ unknown bound of”, otherwise it is P¹ᵢ;
200
+ - if the resulting cv³is different from cv¹ᵢ or cv²ᵢ, or the resulting
201
+ P³ᵢ is different from P¹ᵢ or P²ᵢ, then `const` is added to every cv³ₖ
202
+ for 0 < k < i.
203
+
204
+ where cvʲᵢ and Pʲᵢ are the components of the cv-decomposition of `T`j. A
205
+ prvalue of type `T1` can be converted to type `T2` if the cv-combined
206
+ type of `T1` and `T2` is `T2`.
207
 
208
  [*Note 1*:
209
 
210
  If a program could assign a pointer of type `T**` to a pointer of type
211
  `const` `T**` (that is, if line \#1 below were allowed), a program could
212
+ inadvertently modify a const object (as it is done on line \#2). For
213
  example,
214
 
215
  ``` cpp
216
  int main() {
217
  const char c = 'c';
 
222
  }
223
  ```
224
 
225
  — *end note*]
226
 
227
+ [*Note 2*: Given similar types `T1` and `T2`, this construction ensures
228
+ that both can be converted to the cv-combined type of `T1` and
229
+ `T2`. — *end note*]
230
+
231
+ [*Note 3*: A prvalue of type “pointer to *cv1* `T`” can be converted to
232
  a prvalue of type “pointer to *cv2* `T`” if “*cv2* `T`” is more
233
  cv-qualified than “*cv1* `T`”. A prvalue of type “pointer to member of
234
  `X` of type *cv1* `T`” can be converted to a prvalue of type “pointer to
235
  member of `X` of type *cv2* `T`” if “*cv2* `T`” is more cv-qualified
236
  than “*cv1* `T`”. — *end note*]
237
 
238
+ [*Note 4*: Function types (including those used in
239
+ pointer-to-member-function types) are never cv-qualified
240
+ [[dcl.fct]]. — *end note*]
241
 
242
+ ### Integral promotions <a id="conv.prom">[[conv.prom]]</a>
243
 
244
  A prvalue of an integer type other than `bool`, `char16_t`, `char32_t`,
245
+ or `wchar_t` whose integer conversion rank [[conv.rank]] is less than
246
  the rank of `int` can be converted to a prvalue of type `int` if `int`
247
  can represent all the values of the source type; otherwise, the source
248
  prvalue can be converted to a prvalue of type `unsigned int`.
249
 
250
+ A prvalue of type `char16_t`, `char32_t`, or `wchar_t`
251
+ [[basic.fundamental]] can be converted to a prvalue of the first of the
252
  following types that can represent all the values of its underlying
253
  type: `int`, `unsigned int`, `long int`, `unsigned long int`,
254
  `long long int`, or `unsigned long long int`. If none of the types in
255
  that list can represent all the values of its underlying type, a prvalue
256
  of type `char16_t`, `char32_t`, or `wchar_t` can be converted to a
257
  prvalue of its underlying type.
258
 
259
  A prvalue of an unscoped enumeration type whose underlying type is not
260
+ fixed can be converted to a prvalue of the first of the following types
261
+ that can represent all the values of the enumeration [[dcl.enum]]:
262
+ `int`, `unsigned int`, `long int`, `unsigned long int`, `long long int`,
263
+ or `unsigned long long int`. If none of the types in that list can
264
+ represent all the values of the enumeration, a prvalue of an unscoped
265
+ enumeration type can be converted to a prvalue of the extended integer
266
+ type with lowest integer conversion rank [[conv.rank]] greater than the
267
+ rank of `long long` in which all the values of the enumeration can be
268
+ represented. If there are two such extended types, the signed one is
269
+ chosen.
 
270
 
271
+ A prvalue of an unscoped enumeration type whose underlying type is fixed
272
+ [[dcl.enum]] can be converted to a prvalue of its underlying type.
273
+ Moreover, if integral promotion can be applied to its underlying type, a
274
+ prvalue of an unscoped enumeration type whose underlying type is fixed
275
+ can also be converted to a prvalue of the promoted underlying type.
 
276
 
277
+ A prvalue for an integral bit-field [[class.bit]] can be converted to a
278
+ prvalue of type `int` if `int` can represent all the values of the
279
  bit-field; otherwise, it can be converted to `unsigned int` if
280
  `unsigned int` can represent all the values of the bit-field. If the
281
  bit-field is larger yet, no integral promotion applies to it. If the
282
  bit-field has an enumerated type, it is treated as any other value of
283
  that type for promotion purposes.
 
285
  A prvalue of type `bool` can be converted to a prvalue of type `int`,
286
  with `false` becoming zero and `true` becoming one.
287
 
288
  These conversions are called *integral promotions*.
289
 
290
+ ### Floating-point promotion <a id="conv.fpprom">[[conv.fpprom]]</a>
291
 
292
  A prvalue of type `float` can be converted to a prvalue of type
293
  `double`. The value is unchanged.
294
 
295
  This conversion is called *floating-point promotion*.
296
 
297
+ ### Integral conversions <a id="conv.integral">[[conv.integral]]</a>
298
 
299
  A prvalue of an integer type can be converted to a prvalue of another
300
  integer type. A prvalue of an unscoped enumeration type can be converted
301
  to a prvalue of an integer type.
302
 
 
 
 
 
 
 
 
 
 
 
 
 
303
  If the destination type is `bool`, see  [[conv.bool]]. If the source
304
  type is `bool`, the value `false` is converted to zero and the value
305
  `true` is converted to one.
306
 
307
+ Otherwise, the result is the unique value of the destination type that
308
+ is congruent to the source integer modulo 2ᴺ, where N is the width of
309
+ the destination type.
310
+
311
  The conversions allowed as integral promotions are excluded from the set
312
  of integral conversions.
313
 
314
+ ### Floating-point conversions <a id="conv.double">[[conv.double]]</a>
315
 
316
  A prvalue of floating-point type can be converted to a prvalue of
317
  another floating-point type. If the source value can be exactly
318
  represented in the destination type, the result of the conversion is
319
  that exact representation. If the source value is between two adjacent
 
322
  the behavior is undefined.
323
 
324
  The conversions allowed as floating-point promotions are excluded from
325
  the set of floating-point conversions.
326
 
327
+ ### Floating-integral conversions <a id="conv.fpint">[[conv.fpint]]</a>
328
 
329
  A prvalue of a floating-point type can be converted to a prvalue of an
330
  integer type. The conversion truncates; that is, the fractional part is
331
  discarded. The behavior is undefined if the truncated value cannot be
332
  represented in the destination type.
 
340
  can be represented but the value cannot be represented exactly, it is an
341
  *implementation-defined* choice of either the next lower or higher
342
  representable value.
343
 
344
  [*Note 2*: Loss of precision occurs if the integral value cannot be
345
+ represented exactly as a value of the floating-point
346
+ type. — *end note*]
347
 
348
  If the value being converted is outside the range of values that can be
349
  represented, the behavior is undefined. If the source type is `bool`,
350
  the value `false` is converted to zero and the value `true` is converted
351
  to one.
352
 
353
+ ### Pointer conversions <a id="conv.ptr">[[conv.ptr]]</a>
354
 
355
+ A *null pointer constant* is an integer literal [[lex.icon]] with value
356
+ zero or a prvalue of type `std::nullptr_t`. A null pointer constant can
357
+ be converted to a pointer type; the result is the null pointer value of
358
+ that type [[basic.compound]] and is distinguishable from every other
359
  value of object pointer or function pointer type. Such a conversion is
360
  called a *null pointer conversion*. Two null pointer values of the same
361
  type shall compare equal. The conversion of a null pointer constant to a
362
  pointer to cv-qualified type is a single conversion, and not the
363
+ sequence of a pointer conversion followed by a qualification conversion
364
+ [[conv.qual]]. A null pointer constant of integral type can be converted
365
+ to a prvalue of type `std::nullptr_t`.
366
 
367
  [*Note 1*: The resulting prvalue is not a null pointer
368
  value. — *end note*]
369
 
370
+ A prvalue of type “pointer to cv `T`”, where `T` is an object type, can
371
+ be converted to a prvalue of type “pointer to cv `void`”. The pointer
372
+ value [[basic.compound]] is unchanged by this conversion.
373
 
374
+ A prvalue of type “pointer to cv `D`”, where `D` is a complete class
375
+ type, can be converted to a prvalue of type “pointer to cv `B`”, where
376
+ `B` is a base class [[class.derived]] of `D`. If `B` is an inaccessible
377
+ [[class.access]] or ambiguous [[class.member.lookup]] base class of `D`,
378
+ a program that necessitates this conversion is ill-formed. The result of
379
+ the conversion is a pointer to the base class subobject of the derived
380
+ class object. The null pointer value is converted to the null pointer
381
+ value of the destination type.
382
 
383
+ ### Pointer-to-member conversions <a id="conv.mem">[[conv.mem]]</a>
384
 
385
+ A null pointer constant [[conv.ptr]] can be converted to a
386
+ pointer-to-member type; the result is the *null member pointer value* of
387
+ that type and is distinguishable from any pointer to member not created
388
+ from a null pointer constant. Such a conversion is called a *null member
389
  pointer conversion*. Two null member pointer values of the same type
390
  shall compare equal. The conversion of a null pointer constant to a
391
  pointer to member of cv-qualified type is a single conversion, and not
392
+ the sequence of a pointer-to-member conversion followed by a
393
+ qualification conversion [[conv.qual]].
394
 
395
+ A prvalue of type “pointer to member of `B` of type cv `T`”, where `B`
396
  is a class type, can be converted to a prvalue of type “pointer to
397
+ member of `D` of type cv `T`”, where `D` is a complete class derived
398
+ [[class.derived]] from `B`. If `B` is an inaccessible [[class.access]],
399
+ ambiguous [[class.member.lookup]], or virtual [[class.mi]] base class of
400
+ `D`, or a base class of a virtual base class of `D`, a program that
401
+ necessitates this conversion is ill-formed. The result of the conversion
402
+ refers to the same member as the pointer to member before the conversion
403
+ took place, but it refers to the base class member as if it were a
404
+ member of the derived class. The result refers to the member in `D`’s
405
+ instance of `B`. Since the result has type “pointer to member of `D` of
406
+ type cv `T`”, indirection through it with a `D` object is valid. The
407
+ result is the same as if indirecting through the pointer to member of
408
+ `B` with the `B` subobject of `D`. The null member pointer value is
409
+ converted to the null member pointer value of the destination type.[^8]
 
410
 
411
+ ### Function pointer conversions <a id="conv.fctptr">[[conv.fctptr]]</a>
412
 
413
  A prvalue of type “pointer to `noexcept` function” can be converted to a
414
  prvalue of type “pointer to function”. The result is a pointer to the
415
  function. A prvalue of type “pointer to member of type `noexcept`
416
  function” can be converted to a prvalue of type “pointer to member of
417
+ type function”. The result designates the member function.
418
 
419
  [*Example 1*:
420
 
421
  ``` cpp
422
  void (*p)();
 
426
  void (*q)() noexcept = S(); // error: cannot convert to pointer to noexcept function
427
  ```
428
 
429
  — *end example*]
430
 
431
+ ### Boolean conversions <a id="conv.bool">[[conv.bool]]</a>
432
 
433
+ A prvalue of arithmetic, unscoped enumeration, pointer, or
434
+ pointer-to-member type can be converted to a prvalue of type `bool`. A
435
+ zero value, null pointer value, or null member pointer value is
436
+ converted to `false`; any other value is converted to `true`.
 
 
 
437