[Patch] PR11504: -Wcast-qual and general constness issues with stl_tree.h
Gabriel Dos Reis
gdr@integrable-solutions.net
Sat Jul 26 09:50:00 GMT 2003
Gawain Bolton <gbolton@free.fr> writes:
[...]
| >| + _M_root() { return reinterpret_cast<_Link_type&>(this->_M_header._M_parent); }
| >| +
| >| + _const_Link_type
| >| + _M_root() const { return static_cast<_const_Link_type>(this->_M_header._M_parent); }
| >
| >I do not understand why we would need 'reinterpret_cast' here.
| >If the issue is to safely cast away const-ness, the use const_cast<>.
| >
| No, reinterpret_cast does not cast away constness! Only const_cast
| can cast away constness.
I know that reinterpret_cast<> cannot cast away constness.
(Please, assume I have some working understanding of C++).
The point I was making is that, if the intent is to cast away
const-ness then use const_cast<>. If the intent is to cast from Base
to Derived, then use static_cast (in this particular case). In
particular, there should not be a need for reinterpret_cast.
| There's something I don't understand here. You say reinterpret_cast
| is a "red alert" but don't seem to mind casting away constness.
I think you get that wrong.
| All casting is ugly since it assumes you "know what you're doing" but to
No, you're mistaken.
| me the most dishonest cast is const_cast not reinterpret_cast.
Then, we should agree to disagree. The point has nothing to do with being
honest or dishonest.
| The reason I think const_casts are the most dishonest is that you have
| "lied" to the compiler about a variable be const.
No, nobody lied to the compiler. You should understand that,
const_cast<> is sometimes needed because, something gets accidentally
"const", without the pointed object being effectivelly const. In those
cases, const_cast<> is safe. It has nothing to do with lie.
| I see the other
| types of casts as telling the compiler something like "trust me, I
| know what I'm doing, you can't see that this variable is also this
| type but it is".
Perhaps, but then that applies too to "const_cast".
But, there are great differences between reinterpret_cast<> and
static_cast<>. And here, reinterpret_cast<> is the wrong tool.
Please, do change your patch to take those comments into account.
-- Gaby
More information about the Libstdc++
mailing list