From Jason Turner

[expr.const.cast]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpaw217hef/{from.md → to.md} +29 -32
tmp/tmpaw217hef/{from.md → to.md} RENAMED
@@ -4,63 +4,60 @@ The result of the expression `const_cast<T>(v)` is of type `T`. If `T`
4
  is an lvalue reference to object type, the result is an lvalue; if `T`
5
  is an rvalue reference to object type, the result is an xvalue;
6
  otherwise, the result is a prvalue and the lvalue-to-rvalue
7
  [[conv.lval]], array-to-pointer [[conv.array]], and function-to-pointer
8
  [[conv.func]] standard conversions are performed on the expression `v`.
9
- Conversions that can be performed explicitly using `const_cast` are
10
- listed below. No other conversion shall be performed explicitly using
11
- `const_cast`.
 
12
 
13
  [*Note 1*: Subject to the restrictions in this subclause, an expression
14
  can be cast to its own type using a `const_cast`
15
  operator. — *end note*]
16
 
17
- For two similar types `T1` and `T2` [[conv.qual]], a prvalue of type
18
- `T1` may be explicitly converted to the type `T2` using a `const_cast`
19
- if, considering the qualification-decompositions of both types, each P¹ᵢ
20
- is the same as P²for all i. The result of a `const_cast` refers to the
21
- original entity.
22
-
23
- [*Example 1*:
24
-
25
- ``` cpp
26
- typedef int *A[3]; // array of 3 pointer to int
27
- typedef const int *const CA[3]; // array of 3 const pointer to const int
28
-
29
- CA &&r = A{}; // OK, reference binds to temporary array object
30
- // after qualification conversion to type CA
31
- A &&r1 = const_cast<A>(CA{}); // error: temporary array decayed to pointer
32
- A &&r2 = const_cast<A&&>(CA{}); // OK
33
- ```
34
-
35
- — *end example*]
36
 
37
  For two object types `T1` and `T2`, if a pointer to `T1` can be
38
  explicitly converted to the type “pointer to `T2`” using a `const_cast`,
39
  then the following conversions can also be made:
40
 
41
  - an lvalue of type `T1` can be explicitly converted to an lvalue of
42
  type `T2` using the cast `const_cast<T2&>`;
43
  - a glvalue of type `T1` can be explicitly converted to an xvalue of
44
  type `T2` using the cast `const_cast<T2&&>`; and
45
- - if `T1` is a class type, a prvalue of type `T1` can be explicitly
46
- converted to an xvalue of type `T2` using the cast `const_cast<T2&&>`.
 
 
47
 
48
- The result of a reference `const_cast` refers to the original object if
49
- the operand is a glvalue and to the result of applying the temporary
50
- materialization conversion [[conv.rval]] otherwise.
51
 
52
- A null pointer value [[basic.compound]] is converted to the null pointer
53
- value of the destination type. The null member pointer value
54
- [[conv.mem]] is converted to the null member pointer value of the
55
- destination type.
 
 
 
 
 
 
56
 
57
  [*Note 2*:
58
 
59
  Depending on the type of the object, a write operation through the
60
  pointer, lvalue or pointer to data member resulting from a `const_cast`
61
- that casts away a const-qualifier[^20]
62
 
63
  can produce undefined behavior [[dcl.type.cv]].
64
 
65
  — *end note*]
66
 
 
4
  is an lvalue reference to object type, the result is an lvalue; if `T`
5
  is an rvalue reference to object type, the result is an xvalue;
6
  otherwise, the result is a prvalue and the lvalue-to-rvalue
7
  [[conv.lval]], array-to-pointer [[conv.array]], and function-to-pointer
8
  [[conv.func]] standard conversions are performed on the expression `v`.
9
+ The temporary materialization conversion [[conv.rval]] is not performed
10
+ on `v`, other than as specified below. Conversions that can be performed
11
+ explicitly using `const_cast` are listed below. No other conversion
12
+ shall be performed explicitly using `const_cast`.
13
 
14
  [*Note 1*: Subject to the restrictions in this subclause, an expression
15
  can be cast to its own type using a `const_cast`
16
  operator. — *end note*]
17
 
18
+ For two similar object pointer or pointer to data member types `T1` and
19
+ `T2` [[conv.qual]], a prvalue of type `T1` can be explicitly converted
20
+ to the type `T2` using a `const_cast` if, considering the
21
+ qualification-decompositions of both types, each P¹is the same as P²ᵢ
22
+ for all i. If `v` is a null pointer or null member pointer, the result
23
+ is a null pointer or null member pointer, respectively. Otherwise, the
24
+ result points to or past the end of the same object, or points to the
25
+ same member, respectively, as `v`.
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  For two object types `T1` and `T2`, if a pointer to `T1` can be
28
  explicitly converted to the type “pointer to `T2`” using a `const_cast`,
29
  then the following conversions can also be made:
30
 
31
  - an lvalue of type `T1` can be explicitly converted to an lvalue of
32
  type `T2` using the cast `const_cast<T2&>`;
33
  - a glvalue of type `T1` can be explicitly converted to an xvalue of
34
  type `T2` using the cast `const_cast<T2&&>`; and
35
+ - if `T1` is a class or array type, a prvalue of type `T1` can be
36
+ explicitly converted to an xvalue of type `T2` using the cast
37
+ `const_cast<T2&&>`. The temporary materialization conversion is
38
+ performed on `v`.
39
 
40
+ The result refers to the same object as the (possibly converted)
41
+ operand.
 
42
 
43
+ [*Example 1*:
44
+
45
+ ``` cpp
46
+ typedef int *A[3]; // array of 3 pointer to int
47
+ typedef const int *const CA[3]; // array of 3 const pointer to const int
48
+
49
+ auto &&r2 = const_cast<A&&>(CA{}); // OK, temporary materialization conversion is performed
50
+ ```
51
+
52
+ — *end example*]
53
 
54
  [*Note 2*:
55
 
56
  Depending on the type of the object, a write operation through the
57
  pointer, lvalue or pointer to data member resulting from a `const_cast`
58
+ that casts away a const-qualifier[^18]
59
 
60
  can produce undefined behavior [[dcl.type.cv]].
61
 
62
  — *end note*]
63