You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As per [expr.cast#4], the conversion should undergo that as specified below
The conversions performed by
[...]
a reinterpret_cast followed by a const_cast
can be performed using the cast notation of explicit type conversion. The same semantic restrictions and behaviors apply, ...
In other words, the conversion should firstly use reinterpret_cast, after that, const_cast is used to produce the ultimate result.
As per [expr.reinterpret.cast#7]
An object pointer can be explicitly converted to an object pointer of a different type.60 When a prvalue v of object pointer type is converted to the object pointer type “pointer to cv T”, the result is static_cast<cv T*>(static_cast<cv void*>(v)).
The source type is "pointer to const char" while the destination type is "pointer to int", hence the middle result produced by reinterpret_cast is equivalent to static_cast<int*>(static_cast<void*>(v)) since the cv is empty.
As per [expr.static.cast#1] or [conv.ptr#2]
The static_cast operator shall not cast away constness ([expr.const.cast]).
A prvalue of type “pointer to cv T”, where T is an object type, can be converted to a prvalue of type “pointer to cv void”. The pointer value ([basic.compound]) is unchanged by this conversion.
In either rule, the cv-qualification of the source type shall not be discarded from the destination type. In the nested conversion of the above conversion, namely static_cast<void*>(v), the destination type is void* while the source type is char const*, this conversion discards the const qualification, it seems that "The same semantic restrictions and behaviors apply" does not intend to give the privileged that discards cv-qualification to any conversion produced by explicit type conversion.
However, the example is accepted by all major implementations, it's reasonable to be considered well-formed. Despite that, it cannot be well interpreted by the standard.
The text was updated successfully, but these errors were encountered:
xmh0511
changed the title
Violate "shall not cast constness" principle in the middle process produced by explicit type conversion
Confliction between [expr.cast] and "shall not discard cv-qualification"
Jul 14, 2021
Please see this minimum example
As per [expr.cast#4], the conversion should undergo that as specified below
In other words, the conversion should firstly use
reinterpret_cast
, after that,const_cast
is used to produce the ultimate result.As per [expr.reinterpret.cast#7]
The source type is "pointer to const char" while the destination type is "pointer to int", hence the middle result produced by
reinterpret_cast
is equivalent tostatic_cast<int*>(static_cast<void*>(v))
since the cv is empty.As per [expr.static.cast#1] or [conv.ptr#2]
In either rule, the cv-qualification of the source type shall not be discarded from the destination type. In the nested conversion of the above conversion, namely
static_cast<void*>(v)
, the destination type isvoid*
while the source type ischar const*
, this conversion discards theconst
qualification, it seems that "The same semantic restrictions and behaviors apply" does not intend to give the privileged that discards cv-qualification to any conversion produced by explicit type conversion.However, the example is accepted by all major implementations, it's reasonable to be considered well-formed. Despite that, it cannot be well interpreted by the standard.
The text was updated successfully, but these errors were encountered: