Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > boost::shared_ptr and operator->()

Reply
Thread Tools

boost::shared_ptr and operator->()

 
 
Derek
Guest
Posts: n/a
 
      12-08-2003
Something puzzles me about Boost's shared_ptr implementation:

template <typename T>
class shared_ptr
{
public:
T* operator->() const
{
return p;
}
private:
T* p;
};

Why is operator->() const but the return value is non-const? The
Boost documentation answers this very question as follows:

"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."

I don't understand this answer. Are they referring to using
const_cast to subvert the constness of shallow copy pointers? If so,
that seems like a weak argument for not enforcing const corectness.

Any insights appreciated.


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      12-08-2003
Derek wrote in news:br27r5$28940l$(E-Mail Removed)-berlin.de:

> Something puzzles me about Boost's shared_ptr implementation:
>
> template <typename T>
> class shared_ptr
> {
> public:
> T* operator->() const
> {
> return p;
> }
> private:
> T* p;
> };
>
> Why is operator->() const but the return value is non-const? The
> Boost documentation answers this very question as follows:
>
> "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."
>


Perhapse this will help (for illustration only), what a shared_ptr would
"look" like when const:

template <typename T>
class shared_ptr_const
{
public:
T* const operator->()
{
return p;
}
private:
T* const p;
};

Note that its T * const p *not* T const *p;

T t;
T *v;
T * const c = &t;
v = c; /* shallow copy ?? (losses toplevel const)*/

The (legal) copy above is the same (in a const-correct sense) as:

int const ic = 2;
int j = ic;

Adding (toplevel) const to a return type (as I did in the
illustration above) is mostly useless.

> I don't understand this answer. Are they referring to using
> const_cast to subvert the constness of shallow copy pointers? If so,
> that seems like a weak argument for not enforcing const corectness.
>


Nope, that's not what is being discussed.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
 
Derek
Guest
Posts: n/a
 
      12-08-2003
Rob, your reply makes perfect sense. I overlooked that a const
shared_ptr's member raw pointer is T* const p, not T const *p. Thanks.

> Derek wrote in
> news:br27r5$28940l$(E-Mail Removed)-berlin.de:
>
> > Something puzzles me about Boost's shared_ptr
> > implementation:
> >
> > template <typename T>
> > class shared_ptr
> > {
> > public:
> > T* operator->() const
> > {
> > return p;
> > }
> > private:
> > T* p;
> > };
> >
> > Why is operator->() const but the return value
> > is non-const? The Boost documentation answers
> > this very question as follows:
> >
> > "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."

>
> Perhapse this will help (for illustration only), what a
> shared_ptr would "look" like when const:
>
> template <typename T>
> class shared_ptr_const
> {
> public:
> T* const operator->()
> {
> return p;
> }
> private:
> T* const p;
> };
>
> Note that its T * const p *not* T const *p;
>
> T t;
> T *v;
> T * const c = &t;
> v = c; /* shallow copy ?? (losses toplevel const)*/
>
> The (legal) copy above is the same (in a const-correct
> sense) as:
>
> int const ic = 2;
> int j = ic;
>
> Adding (toplevel) const to a return type (as I did in the
> illustration above) is mostly useless.
>
> > I don't understand this answer. Are they referring to
> > using const_cast to subvert the constness of shallow
> > copy pointers? If so, that seems like a weak argument
> > for not enforcing const corectness.

>
> Nope, that's not what is being discussed.
>
> Rob. -- http://www.victim-prime.dsl.pipex.com/



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
if and and vs if and,and titi VHDL 4 03-11-2007 05:23 AM



Advertisments