Boost logo

Boost :

From: Fernando Cacciola (fernando_cacciola_at_[hidden])
Date: 2002-10-02 07:28:19


----- Original Message -----
From: "David B. Held" <dheld_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Wednesday, October 02, 2002 3:35 AM
Subject: [boost] [smart_ptr] const-correctness as function argument

> It seems to me that smart pointers present a bit of a problem when it
> comes time to pass them as arguments to a function.

I understand the situation but I don't think it is a real problem.
The problem 'appears' from mixing the constantness of the pointer with the
constantness of the pointee.
IOWs:

void foo ( shared_ptr<T> const& x ) ;

is equivalent to:

void foo ( T* const& x ) ;

and in both cases you can mutate the object pointed to by 'x'.

IMO, users should not expect 'deep constantness' from ordinary smart
pointers, because bare pointer don't have it. Furthermore, deep constantess
should not be the common case, meaning that such a behaviour should be
explicit, simply because that's not the way bare pointers. You cannot in
general propagate const all the way down, so it can be 'deep' but not 'the
deepest':

Consider:

struct X { void change() ; } ;
struct Y { void change() ; X* m_X ; };

void foo ( deep_const_smart_ptr<Y> const& p )
{
  p->change() ; // ERROR, OK, deep constantness working.
  p->m_X->change() ; // NO ERROR. deep but not so...
}

This is not to say that there is no use for an additional smart pointer with
deep constantness, although I wouldn't have one in a general library such as
boost because used in general in would lead to the wrong idioms (assuming
deep constanteness as ordinary). I rather use an instance of policy-based
smart pointer.

Fernando Cacciola


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