Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confliction between [expr.cast] and "shall not discard cv-qualification" #4748

Open
xmh0511 opened this issue Jul 14, 2021 · 0 comments
Open

Comments

@xmh0511
Copy link
Contributor

xmh0511 commented Jul 14, 2021

Please see this minimum example

char const* v = 0;
int* identifier = (int*)v;

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.

@xmh0511 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant