tmp/tmphi60q5hd/{from.md → to.md}
RENAMED
|
@@ -1,32 +1,35 @@
|
|
| 1 |
-
### Const cast <a id="expr.const.cast">[[expr.const.cast]]</a>
|
| 2 |
|
| 3 |
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]]
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
-
[*Note 1*: Subject to the restrictions in this
|
| 14 |
may be cast to its own type using a `const_cast`
|
| 15 |
operator. — *end note*]
|
| 16 |
|
| 17 |
-
For two similar types `T1` and `T2`
|
| 18 |
-
`T1` may be explicitly converted to the type `T2` using a `const_cast`
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
[*Example 1*:
|
| 22 |
|
| 23 |
``` cpp
|
| 24 |
typedef int *A[3]; // array of 3 pointer to int
|
| 25 |
typedef const int *const CA[3]; // array of 3 const pointer to const int
|
| 26 |
|
| 27 |
-
CA &&r = A{};
|
|
|
|
| 28 |
A &&r1 = const_cast<A>(CA{}); // error: temporary array decayed to pointer
|
| 29 |
A &&r2 = const_cast<A&&>(CA{}); // OK
|
| 30 |
```
|
| 31 |
|
| 32 |
— *end example*]
|
|
@@ -42,26 +45,25 @@ then the following conversions can also be made:
|
|
| 42 |
- if `T1` is a class type, a prvalue of type `T1` can be explicitly
|
| 43 |
converted to an xvalue of type `T2` using the cast `const_cast<T2&&>`.
|
| 44 |
|
| 45 |
The result of a reference `const_cast` refers to the original object if
|
| 46 |
the operand is a glvalue and to the result of applying the temporary
|
| 47 |
-
materialization conversion
|
| 48 |
|
| 49 |
-
A null pointer value
|
| 50 |
-
value of the destination type. The null member pointer value
|
| 51 |
-
[[conv.mem]]
|
| 52 |
destination type.
|
| 53 |
|
| 54 |
[*Note 2*: Depending on the type of the object, a write operation
|
| 55 |
through the pointer, lvalue or pointer to data member resulting from a
|
| 56 |
-
`const_cast` that casts away a const-qualifier[^
|
| 57 |
-
undefined behavior
|
| 58 |
|
| 59 |
A conversion from a type `T1` to a type `T2` *casts away constness* if
|
| 60 |
-
`T1` and `T2` are different, there is a cv-decomposition
|
| 61 |
-
|
| 62 |
-
cv-decomposition of the form
|
| 63 |
|
| 64 |
and there is no qualification conversion that converts `T1` to
|
| 65 |
|
| 66 |
Casting from an lvalue of type `T1` to an lvalue of type `T2` using an
|
| 67 |
lvalue reference cast or casting from an expression of type `T1` to an
|
|
|
|
| 1 |
+
#### Const cast <a id="expr.const.cast">[[expr.const.cast]]</a>
|
| 2 |
|
| 3 |
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 |
may 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 cv-decompositions of both types, each P¹ᵢ is the
|
| 20 |
+
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*]
|
|
|
|
| 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*: Depending on the type of the object, a write operation
|
| 58 |
through the pointer, lvalue or pointer to data member resulting from a
|
| 59 |
+
`const_cast` that casts away a const-qualifier[^20] may produce
|
| 60 |
+
undefined behavior [[dcl.type.cv]]. — *end note*]
|
| 61 |
|
| 62 |
A conversion from a type `T1` to a type `T2` *casts away constness* if
|
| 63 |
+
`T1` and `T2` are different, there is a cv-decomposition [[conv.qual]]
|
| 64 |
+
of `T1` yielding *n* such that `T2` has a cv-decomposition of the form
|
|
|
|
| 65 |
|
| 66 |
and there is no qualification conversion that converts `T1` to
|
| 67 |
|
| 68 |
Casting from an lvalue of type `T1` to an lvalue of type `T2` using an
|
| 69 |
lvalue reference cast or casting from an expression of type `T1` to an
|