According to [expr.cast]/4, a C-style cast tries the following casts in order:
const_cast
static_cast
static_cast
followed byconst_cast
reinterpret_cast
reinterpret_cast
followed byconst_cast
The following cast is well-formed:
const_cast<int&>(static_cast<const int&>(0))
Yet both GCC and Clang reject the cast (int&)0
. Why?
const int&
it was not rejected. I'll have to ponder. – Unsetconst int&
is fine because of lifetime extension – Doorkeeperconst&
works – Doorkeeperstatic_cast<int &>0
is rejected by the compiler, because that is what(int &)0
resolves to. And the question I linked to as a dup addresses that. – Fireflyfloat* p; (int*)p;
even though the static cast would be rejected---because it interprets it as a reinterpret cast instead. But in my case, the (third) alternative interpretation is not used. – Recriminate&
creates an alias to (the name of) an lvalue. The number 0 is not a name or an lvalue. – Rhyndstatic_cast
is considered “possible” until [expr.static.cast]/1 forbids that it “cast away constness”. But that would seem to prevent ever using interpretation #3. – Funke