Boost logo

Boost :

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


----- Original Message -----
From: "Yitzhak Sapir" <yitzhaks_at_[hidden]>
To: "Boost mailing list" <boost_at_[hidden]>
Sent: Wednesday, October 02, 2002 10:04 AM
Subject: RE: [boost] [smart_ptr] const-correctness as function argument

> > -----Original Message-----
> > From: David Abrahams [mailto:dave_at_[hidden]]
>
> > Why is everyone barking up this tree instead of using
> > shared_ptr<T const>
> > "as god intended"?
>
> I don't have a problem with shared_ptr as it is, (and in my post, I
covered reasons for leaving shared_ptr's as they are -- I don't support the
viewpoint that shared_ptr should have two ->/* functions). But I use
scoped_ptr's to hold the pointers to a private implementation struct. When
I do this, I don't want to have non-const access to the struct in const
functions. Having a scoped_ptr<T const> won't help me in this regard
because I do want non-const access in non-const functions.

As I said in the recent c.l.cpp.m thread on this: You can achieve deep
constantness from any smart pointer on a use-case basis with a helper
function:

template<typename T>
inline T const* get_deep_const_pointer(shared_ptr<T> const & p)
{
    return p.get();
}

template<typename T>
inline T* get_deep_const_pointer(shared_ptr<T>& p)
{
    return p.get();
}

void foo ( shared_ptr<T> const& x )
{
  get_deep_const_pointer(x)->change() ; // Error.
}

Fernando Cacciola


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