Boost logo

Boost :

From: Rob Stewart (stewart_at_[hidden])
Date: 2004-09-20 14:50:24


From: "Jonathan Turkanis" <technews_at_[hidden]>
> "David Abrahams" <dave_at_[hidden]> wrote in message
> news:umzzl3pvk.fsf_at_boost-consulting.com...
> > Richard Hadsell <hadsell_at_[hidden]> writes:
> > > Jonathan Turkanis wrote:
> > >
> > >>When I was writing it I initially typed 'const_cast', but then thought to
> > >>myself: why use my big guns if I don't have to? ;-)
> > >>
> > > For clarity -- to tell the reader that you intended to modify the
> > > const-ness.
> >
> > static_cast is a bigger gun than const_cast. At least const_cast
> > cant violate data layout, and unless there is actually a constant
> > object, won't result in undefined behavior.
>
> Good point. I guess part of the reason I avoid const_cast whenever possible is
> that people look upon it suspiciously, even if it's only adding
> cv-qualification. Maybe people should be more suspicious of static_cast.

As Dave points out, there is much more possible with static_cast,
so it should be viewed with suspicion. Casting away constness is
always worth a second look (including asking why mutable wasn't
used), and adding const is certainly unusual. Therefore, I
understand why you would consider const_cast to be more of a
problem than static_cast.

The bottom line is any cast should be considered suspect and
should, in most contexts, be documented. (That allows a future
maintainer the opportunity to remove it if an alternative is
found.)

-- 
Rob Stewart                           stewart_at_[hidden]
Software Engineer                     http://www.sig.com
Susquehanna International Group, LLP  using std::disclaimer;

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk