Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-01-03 09:37:32

"Thorsten Ottosen" <nesotto_at_[hidden]> writes:

> "David B. Held" <dheld_at_[hidden]> wrote in message
> news:bt5trp$s2m$
>> "Thorsten Ottosen" <nesotto_at_[hidden]> wrote in message
>> news:bt5ip0$ge1$
>> > [...]
>> > Having propagation of constness makes a lot of sense and
>> > would certainly make the smart pointers a lot smarter.
>> At first, I thought so too. But then I thought about what raw pointers
>> do, and I decided that it was better to mimic the built-ins. If you
>> always propagate constness, then you can't have a const ptr to a
>> non-const object. Maybe that doesn't bother you, but perhaps there
>> is a scenario in which you want to be able to modify the pointee in
>> a function, but you don't want to accidentally reset() or release() the
>> pointer in that function. Propagation of constness eliminates this
>> possibility.
> Why? Can't you cast the constness away?

A const_cast is an open door to undefined behavior. The language is
designed to make most casts unneccessary by providing a type system
which separates the constness of an object from the constness of
objects *reachable* from that object. That's why char const* and
char*const are different types.

> I really don't understand why correctness should not be the default
> and then let those who want it do the hacks?

It's the system which requires const_cast that is the hack. The
language is correct and consistent as it stands.

It would only make sense in C++ to make a pointee's mutability depend
on the mutability of the pointer if we were going to think of the
pointee as being part of the pointer. For that application, we have
boost::optional. Pointers should probable remain pointer-like. In
other words, they simply refer to other objects but do not contain

Dave Abrahams
Boost Consulting

Boost list run by bdawes at, gregod at, cpdaniel at, john at