Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr and container iterators
From: Johannes Totz (johannes_at_[hidden])
Date: 2012-04-17 11:56:27


On 16/04/2012 20:06, Igor R wrote:
>> just started playing with boost::shared_ptr.
>> How do I get a const iterator pointing to a shared_ptr pointing to a
>> const object? See code below, I'd like to prevent a MyItem being changed
>> through const iterators.
>
> IIUC, the following answers your question:
> http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/shared_ptr.htm#FAQ
> <<Q. Why is operator->() const, but its return value is a non-const
> pointer to the element type?
> 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".>>

Got a recommendation as to how to get this const-correct? I'm kinda
clueless right now... Or does the above faq mean it just cant be done?

struct MyItem
{
        int value;
};

struct MyContainer
{
protected:
        typedef std::set<boost::shared_ptr<MyItem> > MySet;
        MySet set;

public:
        MySet::iterator stuff_begin();

        // should not change ptr nor MyItem nor set
        XXX::const_iterator stuff_begin() const;
};


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net