From Jason Turner

[expr.const.cast]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp3h02oe5m/{from.md → to.md} +37 -48
tmp/tmp3h02oe5m/{from.md → to.md} RENAMED
@@ -8,22 +8,30 @@ otherwise, the result is a prvalue and the lvalue-to-rvalue (
8
  function-to-pointer ([[conv.func]]) standard conversions are performed
9
  on the expression `v`. Conversions that can be performed explicitly
10
  using `const_cast` are listed below. No other conversion shall be
11
  performed explicitly using `const_cast`.
12
 
13
- Subject to the restrictions in this section, an expression may be cast
14
- to its own type using a `const_cast` operator.
 
15
 
16
- For two pointer types `T1` and `T2` where
 
 
17
 
18
- and
19
 
20
- where `T` is any object type or the `void` type and where
21
- $\mathit{cv}_{1,k}$ and $\mathit{cv}_{2,k}$ may be different
22
- cv-qualifications, a prvalue of type `T1` may be explicitly converted to
23
- the type `T2` using a `const_cast`. The result of a pointer `const_cast`
24
- refers to the original object.
 
 
 
 
 
25
 
26
  For two object types `T1` and `T2`, if a pointer to `T1` can be
27
  explicitly converted to the type “pointer to `T2`” using a `const_cast`,
28
  then the following conversions can also be made:
29
 
@@ -32,60 +40,41 @@ then the following conversions can also be made:
32
  - a glvalue of type `T1` can be explicitly converted to an xvalue of
33
  type `T2` using the cast `const_cast<T2&&>`; and
34
  - if `T1` is a class type, a prvalue of type `T1` can be explicitly
35
  converted to an xvalue of type `T2` using the cast `const_cast<T2&&>`.
36
 
37
- The result of a reference `const_cast` refers to the original object.
38
-
39
- For a `const_cast` involving pointers to data members, multi-level
40
- pointers to data members and multi-level mixed pointers and pointers to
41
- data members ([[conv.qual]]), the rules for `const_cast` are the same
42
- as those used for pointers; the “member” aspect of a pointer to member
43
- is ignored when determining where the cv-qualifiers are added or removed
44
- by the `const_cast`. The result of a pointer to data member `const_cast`
45
- refers to the same member as the original (uncast) pointer to data
46
- member.
47
 
48
  A null pointer value ([[conv.ptr]]) is converted to the null pointer
49
  value of the destination type. The null member pointer value (
50
  [[conv.mem]]) is converted to the null member pointer value of the
51
  destination type.
52
 
53
- Depending on the type of the object, a write operation through the
54
- pointer, lvalue or pointer to data member resulting from a `const_cast`
55
- that casts away a const-qualifier[^15] may produce undefined behavior (
56
- [[dcl.type.cv]]).
57
 
58
- The following rules define the process known as *casting away
59
- constness*. In these rules `Tn ` and `Xn ` represent types. For two
60
- pointer types:
 
61
 
62
- casting from `X1` to `X2` casts away constness if, for a non-pointer
63
- type `T` there does not exist an implicit conversion (Clause  [[conv]])
64
- from:
65
-
66
- 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
70
  xvalue of type `T2` using an rvalue reference cast casts away constness
71
  if a cast from a prvalue of type “pointer to `T1`” to the type “pointer
72
  to `T2`” casts away constness.
73
 
74
- Casting from a prvalue of type “pointer to data member of `X` of type
75
- `T1`” to the type “pointer to data member of `Y` of type `T2`” casts
76
- away constness if a cast from a prvalue of type “pointer to `T1`” to the
77
- type “pointer to `T2`” casts away constness.
78
-
79
- For multi-level pointer to members and multi-level mixed pointers and
80
- pointer to members ([[conv.qual]]), the member aspect of a pointer to
81
- member level is ignored when determining if a `const` cv-qualifier has
82
- been cast away.
83
-
84
- some conversions which involve only changes in cv-qualification cannot
85
- be done using `const_cast.` For instance, conversions between pointers
86
- to functions are not covered because such conversions lead to values
87
- whose use causes undefined behavior. For the same reasons, conversions
88
- between pointers to member functions, and in particular, the conversion
89
- from a pointer to a const member function to a pointer to a non-const
90
- member function, are not covered.
91
 
 
8
  function-to-pointer ([[conv.func]]) standard conversions are performed
9
  on the expression `v`. Conversions that can be performed explicitly
10
  using `const_cast` are listed below. No other conversion shall be
11
  performed explicitly using `const_cast`.
12
 
13
+ [*Note 1*: Subject to the restrictions in this section, 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
+ The result of a `const_cast` refers to the original entity.
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{}; // OK, reference binds to temporary array object after qualification conversion to type CA
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*]
33
 
34
  For two object types `T1` and `T2`, if a pointer to `T1` can be
35
  explicitly converted to the type “pointer to `T2`” using a `const_cast`,
36
  then the following conversions can also be made:
37
 
 
40
  - a glvalue of type `T1` can be explicitly converted to an xvalue of
41
  type `T2` using the cast `const_cast<T2&&>`; and
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 ([[conv.rval]]) otherwise.
 
 
 
 
 
 
 
48
 
49
  A null pointer value ([[conv.ptr]]) is converted to the null pointer
50
  value of the destination type. The null member pointer value (
51
  [[conv.mem]]) is converted to the null member pointer value of the
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[^15] may produce
57
+ undefined behavior ([[dcl.type.cv]]). — *end note*]
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
+ [[conv.qual]]) of `T1` yielding *n* such that `T2` has a
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
68
  xvalue of type `T2` using an rvalue reference cast casts away constness
69
  if a cast from a prvalue of type “pointer to `T1`” to the type “pointer
70
  to `T2`” casts away constness.
71
 
72
+ [*Note 3*: Some conversions which involve only changes in
73
+ cv-qualification cannot be done using `const_cast.` For instance,
74
+ conversions between pointers to functions are not covered because such
75
+ conversions lead to values whose use causes undefined behavior. For the
76
+ same reasons, conversions between pointers to member functions, and in
77
+ particular, the conversion from a pointer to a const member function to
78
+ a pointer to a non-const member function, are not
79
+ covered. *end note*]
 
 
 
 
 
 
 
 
 
80