Init
This commit is contained in:
136
cppdraft/expr/const/cast.md
Normal file
136
cppdraft/expr/const/cast.md
Normal file
@@ -0,0 +1,136 @@
|
||||
[expr.const.cast]
|
||||
|
||||
# 7 Expressions [[expr]](./#expr)
|
||||
|
||||
## 7.6 Compound expressions [[expr.compound]](expr.compound#expr.const.cast)
|
||||
|
||||
### 7.6.1 Postfix expressions [[expr.post]](expr.post#expr.const.cast)
|
||||
|
||||
#### 7.6.1.11 Const cast [expr.const.cast]
|
||||
|
||||
[1](#1)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L5051)
|
||||
|
||||
The result of the expression const_cast<T>(v) is of typeT[.](#1.sentence-1)
|
||||
|
||||
If T is an lvalue reference to object type, the result is an
|
||||
lvalue;
|
||||
if T is an rvalue reference to object type, the result is an xvalue;
|
||||
otherwise, the result is a prvalue and the[lvalue-to-rvalue](conv.lval "7.3.2 Lvalue-to-rvalue conversion [conv.lval]"), [array-to-pointer](conv.array "7.3.3 Array-to-pointer conversion [conv.array]"),
|
||||
and [function-to-pointer](conv.func "7.3.4 Function-to-pointer conversion [conv.func]") standard conversions are
|
||||
performed on the expression v[.](#1.sentence-2)
|
||||
|
||||
The [temporary materialization conversion](conv.rval "7.3.5 Temporary materialization conversion [conv.rval]") is not
|
||||
performed on v, other than as specified below[.](#1.sentence-3)
|
||||
|
||||
Conversions that can be performed explicitly usingconst_cast are listed below[.](#1.sentence-4)
|
||||
|
||||
No other conversion shall be
|
||||
performed explicitly using const_cast[.](#1.sentence-5)
|
||||
|
||||
[2](#2)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L5068)
|
||||
|
||||
[*Note [1](#note-1)*:
|
||||
|
||||
Subject to the restrictions in this subclause, an expression can be cast
|
||||
to its own type using a const_cast operator[.](#2.sentence-1)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[3](#3)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L5074)
|
||||
|
||||
For two similar object pointer or pointer to data member typesT1 and T2 ([[conv.qual]](conv.qual "7.3.6 Qualification conversions")),
|
||||
a prvalue of type T1 can be explicitly
|
||||
converted to the type T2 using a const_cast if, considering the qualification-decompositions of both types,
|
||||
each P1i is the same as P2i for all i[.](#3.sentence-1)
|
||||
|
||||
If v is a null pointer or null member pointer,
|
||||
the result is a null pointer or null member pointer, respectively[.](#3.sentence-2)
|
||||
|
||||
Otherwise, the result points to or past the end of the same object, or
|
||||
points to the same member, respectively, as v[.](#3.sentence-3)
|
||||
|
||||
[4](#4)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L5086)
|
||||
|
||||
For two object types T1 and T2, if a pointer to T1 can
|
||||
be explicitly converted to the type âpointer to T2â using aconst_cast, then the following conversions can also be made:
|
||||
|
||||
- [(4.1)](#4.1)
|
||||
|
||||
an lvalue of type T1 can be explicitly converted to an lvalue
|
||||
of type T2 using the cast const_cast<T2&>;
|
||||
|
||||
- [(4.2)](#4.2)
|
||||
|
||||
a glvalue of type T1 can be explicitly converted to an xvalue
|
||||
of type T2 using the cast const_cast<T2&&>; and
|
||||
|
||||
- [(4.3)](#4.3)
|
||||
|
||||
if T1 is a class or array type,
|
||||
a prvalue of type T1 can be
|
||||
explicitly converted to an xvalue of type T2 using the castconst_cast<T2&&>[.](#4.sentence-1)
|
||||
The temporary materialization conversion is performed on v[.](#4.3.sentence-2)
|
||||
|
||||
The result refers to the same object as the (possibly converted) operand[.](#4.sentence-2)
|
||||
|
||||
[*Example [1](#example-1)*: typedef int *A[3]; // array of 3 pointer to inttypedef const int *const CA[3]; // array of 3 const pointer to const intauto &&r2 = const_cast<A&&>(CA{}); // OK, temporary materialization conversion is performed â *end example*]
|
||||
|
||||
[5](#5)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L5114)
|
||||
|
||||
[*Note [2](#note-2)*:
|
||||
|
||||
Depending on the type of the object, a write operation through the
|
||||
pointer, lvalue or pointer to data member resulting from aconst_cast that casts away a const-qualifier[56](#footnote-56 "const_cast is not limited to conversions that cast away a const-qualifier.") can produce undefined behavior ([[dcl.type.cv]](dcl.type.cv "9.2.9.2 The cv-qualifiers"))[.](#5.sentence-1)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[6](#6)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L5127)
|
||||
|
||||
A conversion from a type T1 to a type T2[*casts away constness*](#def:casting_away_constness "7.6.1.11 Const cast [expr.const.cast]") if T1 and T2 are different,
|
||||
there is a qualification-decomposition ([[conv.qual]](conv.qual "7.3.6 Qualification conversions")) of T1 yielding *n* such thatT2 has a qualification-decomposition of the formcv20 P20 cv21 P21 ⯠cv2nâ1 P2nâ1 cv2n U2, and there is no qualification conversion that converts T1 tocv20 P10 cv21 P11 ⯠cv2nâ1 P1nâ1 cv2n U1[.](#6.sentence-1)
|
||||
|
||||
[7](#7)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L5143)
|
||||
|
||||
Casting from an lvalue of type T1 to an lvalue of typeT2 using an lvalue reference cast
|
||||
or casting from an expression of type T1 to an xvalue of type T2 using
|
||||
an rvalue reference cast
|
||||
casts away constness if a cast from a prvalue of type âpointer to T1â to the type âpointer toT2â casts away constness[.](#7.sentence-1)
|
||||
|
||||
[8](#8)
|
||||
|
||||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/expressions.tex#L5151)
|
||||
|
||||
[*Note [3](#note-3)*:
|
||||
|
||||
Some conversions which involve only changes in cv-qualification cannot
|
||||
be done using const_cast[.](#8.sentence-1)
|
||||
|
||||
For instance, conversions between
|
||||
pointers to functions are not covered because such conversions lead to
|
||||
values whose use causes undefined behavior[.](#8.sentence-2)
|
||||
|
||||
For the same reasons,
|
||||
conversions between pointers to member functions, and in particular, the
|
||||
conversion from a pointer to a const member function to a pointer to a
|
||||
non-const member function, are not covered[.](#8.sentence-3)
|
||||
|
||||
â *end note*]
|
||||
|
||||
[56)](#footnote-56)[56)](#footnoteref-56)
|
||||
|
||||
const_cast is not limited to conversions that cast away a
|
||||
const-qualifier[.](#footnote-56.sentence-1)
|
||||
Reference in New Issue
Block a user