<div class="gmail_quote">On Tue, Apr 17, 2012 at 8:56 AM, Johannes Totz <span dir="ltr">&lt;<a href="mailto:johannes@jo-t.de">johannes@jo-t.de</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 16/04/2012 20:06, Igor R wrote:<br>
&gt;&gt; just started playing with boost::shared_ptr.<br>
&gt;&gt; How do I get a const iterator pointing to a shared_ptr pointing to a<br>
&gt;&gt; const object? See code below, I&#39;d like to prevent a MyItem being changed<br>
&gt;&gt; through const iterators.<br>
&gt;<br>
&gt; IIUC, the following answers your question:<br>
&gt; <a href="http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/shared_ptr.htm#FAQ" target="_blank">http://www.boost.org/doc/libs/1_49_0/libs/smart_ptr/shared_ptr.htm#FAQ</a><br>
&gt; &lt;&lt;Q. Why is operator-&gt;() const, but its return value is a non-const<br>
&gt; pointer to the element type?<br>
&gt; A. Shallow copy pointers, including raw pointers, typically don&#39;t<br>
&gt; propagate constness. It makes little sense for them to do so, as you<br>
&gt; can always obtain a non-const pointer from a const one and then<br>
&gt; proceed to modify the object through it.shared_ptr is &quot;as close to raw<br>
&gt; pointers as possible but no closer&quot;.&gt;&gt;<br>
<br>
Got a recommendation as to how to get this const-correct? I&#39;m kinda<br>
clueless right now... Or does the above faq mean it just cant be done?<br>
<br>
<br>
struct MyItem<br>
{<br>
        int     value;<br>
};<br>
<br>
struct MyContainer<br>
{<br>
protected:<br>
        typedef std::set&lt;boost::shared_ptr&lt;MyItem&gt; &gt;    MySet;<br>
        MySet   set;<br>
<br>
public:<br>
        MySet::iterator stuff_begin();<br>
<br>
        // should not change ptr nor MyItem nor set<br>
        XXX::const_iterator stuff_begin() const;<br>
};<br></blockquote><div><br>Maybe you can use a transform_iterator to map the shared_ptr&lt;MyItem&gt; to a shared_ptr&lt;MyItem const&gt; (if such a conversion exists, I&#39;m not sure)?<br><br>- Jeff<br><br></div></div>

