[expr.const.cast] (original) (raw)
7 Expressions [expr]
7.6 Compound expressions [expr.compound]
7.6.1 Postfix expressions [expr.post]
7.6.1.11 Const cast [expr.const.cast]
The result of the expression const_cast<T>(v) is of typeT.
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 thelvalue-to-rvalue, array-to-pointer, and function-to-pointer standard conversions are performed on the expression v.
Conversions that can be performed explicitly usingconst_cast are listed below.
No other conversion shall be performed explicitly using const_cast.
[Note 1:
Subject to the restrictions in this subclause, an expression can be cast to its own type using a const_cast operator.
— _end note_]
For two similar object pointer or pointer to data member typesT1 and T2 ([conv.qual]), a prvalue of type T1 can be explicitly converted to the type T2 using a const_castif, considering the qualification-decompositions of both types, each is the same as for all i.
If v is a null pointer or null member pointer, the result is a null pointer or null member pointer, respectively.
Otherwise, the result points to or past the end of the same object, or points to the same member, respectively, as v.
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:
- an lvalue of type T1 can be explicitly converted to an lvalue of type T2 using the cast const_cast<T2&>;
- a glvalue of type T1 can be explicitly converted to an xvalue of type T2 using the cast const_cast<T2&&>; and
- 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&&>.
The temporary materialization conversion is performed on v.
The result refers to the same object as the (possibly converted) operand.
[Example 1: typedef int *A[3]; typedef const int *const CA[3]; auto &&r2 = const_cast<A&&>(CA{}); — _end example_]
[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-qualifier56can produce undefined behavior ([dcl.type.cv]).
— _end note_]
A conversion from a type T1 to a type T2 casts away constnessif T1 and T2 are different, there is a qualification-decomposition ([conv.qual]) of T1yielding n such thatT2 has a qualification-decomposition of the form ⋯ ,and there is no qualification conversion that converts T1 to ⋯ .
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.
[Note 3:
Some conversions which involve only changes in cv-qualification cannot be done using const_cast.
For instance, conversions between pointers to functions are not covered because such conversions lead to values whose use causes undefined behavior.
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.
— _end note_]