This question concerns something I noticed in the C++ spec when I was trying to answer this earlier, intriguing question about C-style casts and type conversions.
The C++ spec talks about C-style casts in ยง5.4. It says that the cast notation will try the following casts, in this order, until one is found that is valid:
const_cast
static_cast
static_cast
followed byconst_cast
reinterpret_cast
reinterpret_cast
followed byconst_cast
.
While I have a great intuitive idea of what it means to use a static_cast
followed by a const_cast
(for example, to convert a const Derived*
to a Base*
by going through a const_cast<Base*>(static_cast<const Base*>(expr))
), I don't see any wording in the spec saying how, specifically, the types used in the static_cast
/const_cast
series are to be deduced. In the case of simple pointers it's not that hard, but as seen in the linked question the cast might succeed if an extra const
is introduced in one place and removed in another.
Are there any rules governing how a compiler is supposed to determine what types to use in the casting chain? If so, where are they? If not, is this a defect in the language, or are there sufficient implicit rules to uniquely determine all the possible casts to try?