boost::shared_ptr and operator->()

Discussion in 'C++' started by Derek, Dec 8, 2003.

  1. Derek

    Derek Guest

    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.
    Derek, Dec 8, 2003
    #1
    1. Advertising

  2. Derek wrote in news:br27r5$28940l$-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/
    Rob Williscroft, Dec 8, 2003
    #2
    1. Advertising

  3. Derek

    Derek Guest

    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$-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/
    Derek, Dec 8, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryan Mitchley
    Replies:
    6
    Views:
    11,122
    Maxim Yegorushkin
    Jun 26, 2004
  2. =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=

    boost::weak_ptr and shared_ptr pointers from "this"

    =?UTF-8?B?UmFmYcWCIE1haiBSYWYyNTY=?=, Apr 5, 2006, in forum: C++
    Replies:
    2
    Views:
    1,215
    Joe Gottman
    Apr 6, 2006
  3. Toby Bradshaw
    Replies:
    6
    Views:
    1,724
    Kai-Uwe Bux
    Jun 2, 2006
  4. Colin Caughie
    Replies:
    1
    Views:
    694
    Shooting
    Aug 29, 2006
  5. bb
    Replies:
    7
    Views:
    775
Loading...

Share This Page