Boost logo

Boost :

Subject: Re: [boost] Interest in a protected pointer
From: Dmitry Goncharov (dgoncharov_at_[hidden])
Date: 2009-02-11 03:41:26


Ray Logel wrote:
>
> Is there any interest in pointer wrapper similar to scoped_ptr or
> shared_ptr which can be used for pointer member variable which can the
> referenced value can't be modified by non const functions? This class
> wouldn't be responsible for any memory management and could have a
> shared pointer version as well.
>
> For example:
> template<typename T>
> class protected_ptr
> {
> T * ptr;
>
> // ...
> T * get();
> const T * get() const;
>
> // ... including operator* and operator->
> }
>
> class Foo
> {
> protected_ptr<Bar> _bar;
>
> void Func1()
> {
> _bar->NonConstFunc();
> _bar->ConstFunc();
> }
>
> void Func2() const
> {
> _bar->NonConstFunc(); // Compile error
> _bar->ConstFunc(); // Ok
> }
> }
>
> Ray Logel
>
>
The could be useful to propagate constness, however it looks like the
boost smart pointers deliberately dont do that.
The following is an excerpt from shared_ptr's faq
(http://www.boost.org/doc/libs/1_38_0/libs/smart_ptr/shared_ptr.htm#functions):

"A. Shallow copy pointers, including raw pointers, typically don't
propagate constness. It makes little sense for them to do so, as you can
always obtain a non-const pointer from a const one and then proceed to
modify the object through it.shared_ptr is "as close to raw pointers as
possible but no closer"."

BR, Dmitry


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