<div class="gmail_quote">On 13 December 2011 10:07, Igor R <span dir="ltr">&lt;<a href="mailto:boost.lists@gmail.com">boost.lists@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="im">&gt; -- I&#39;m trying to use shared_ptr in a stl set, but am having trouble with find()<br>
&gt;   Any help?  Documentation on this seems hard to find.<br>
&gt;<br>
&gt; -- Also, are the overloaded &lt; and == useful or correct here?<br>
<br>
<br>
</div>No, std algorithms won&#39;t call your comparison operators, because those<br>
of boost::shared_ptr do not compare pointees.<br>
You can define in your cpp something like this (operator ==() is not<br>
needed for your particular case):<br></blockquote><div><br>operator&lt;() is needed for set sorting, but he does need operator==() for the find() to work.<br><br></div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">


namespace boost<br>
{<br>
        template&lt;class T&gt;<br>
        bool operator &lt;(const shared_ptr&lt;T&gt; &amp;lhs, const shared_ptr&lt;T&gt; &amp;rhs)<br>
        {<br>
                if (!lhs &amp;&amp; !rhs)<br>
                        return false;<br>
                return *lhs &lt; *rhs;<br>
        }<br>
}<br>
<br>
Then move your operator &lt;() out of Node class, and your programm will work.<br></blockquote><div> </div>Your program might work, but anyone else who might include your operator&lt;() definition in their own code will be extremely surprised when this doesn&#39;t work as expected:<br>

   ASSERT_FALSE(shared_ptr&lt;Foo&gt;(new Foo()) == shared_ptr&lt;Foo&gt;(new Foo()));</div><br>When I compare 2 pointers, smart or raw, I want to know if the pointers are equal not the subobjects. This is the reason I would strongly recommend not doing this, regardless of whether this code will be used by others or not. Follow POLS - Principle Of Least Surprise; to override the equality operator for all shared_ptr&lt;&gt; instances would be staggeringly surprising, but even for 1 is unexpected.<br>

<br>A possible alternative, if you didn&#39;t wish to use boost::ptr_set for some reason, would be to provide your own less than comparator for the set type, though that still leaves you with the custom &#39;find&#39; behaviour. For that you could use std::find_if, but it won&#39;t give you as good performance as set&#39;s own methods (linear vs. logarithmic IIRC).<br>

I guess another alternative would be a shared_ptr wrapper that does the dereferencing compare as you want, but that may prove tricky to do. I&#39;d still recommend ptr_container.<br clear="all"><br>-- <br>Rob Desbois<br>
Blog: <a href="http://theotherbranch.wordpress.com/" target="_blank">http://theotherbranch.wordpress.com/</a><br>
&quot;I disapprove of what you say, but I’ll defend to the death your right to say it&quot;, Voltaire<br>

