This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [Patch] PR11504: -Wcast-qual and general constness issues with stl_tree.h


On Sat, Jul 26, 2003 at 11:12:12AM +0200, Gawain Bolton wrote:
> Gabriel Dos Reis wrote:
> 
> >Gawain Bolton <gbolton@free.fr> writes:
> >
> >|    1. I do not like the lack of symmetry with the casts for const vs.
> >|       non-const functions.
> >|       Non-const versions often require a reinterpret_cast whereas const
> >|       versions can use static_cast.
> >
> >Use of reinterpret_cast is a red alert.
> >
> Let's put this in perspective.  All C-style casts were replaced, in some 
> cases with reinterpret_cast where required.  C-style casts are even 
> worse than reinterpret_cast because they can also cast away constness 
> where reinterpret_cast cannot.  So the patch makes things better, not 
> worse, with respect to casting.

The problem with reinterpret_cast<> is that in many circumstances it 
produces undefined behavior.  It's not easy to tell by looking whether
that is true in any particular case.  Therefore, it's always better to
use (as necessary) a sequence of const_cast<> and static_cast<>, instead
of reinterpret_cast<> where it is possible at all.  Of course, in cases 
where you are beating raw bytes into shape, nothing but reinterpret_cast<> 
will do, but I don't think that is going on here.

Nathan Myers
ncm-nospam@cantrip.org


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]