Boost C++ Libraries: Ticket #6237: unordered_set behaves differently from 1.46 to 1.48 https://svn.boost.org/trac10/ticket/6237 <p> The following code runs OK on 1.46 but fails on 1.48. The correct output is "1". </p> <pre class="wiki">// gcc -Iboost -lstdc++ test1.cpp #include &lt;iostream&gt; #include &lt;boost/smart_ptr.hpp&gt; #include &lt;boost/unordered_set.hpp&gt; namespace ns { typedef boost::shared_ptr&lt;class c&gt; cptr; typedef boost::unordered_set&lt;cptr&gt; cset; class c { public: c() { } }; bool operator==(const cptr &amp;c1, const cptr &amp;c2) { return true; } } int main(int argc, char **argv) { ns::cptr bb1(new ns::c()); ns::cptr bb2(new ns::c()); ns::cset cs; cs.insert(boost::dynamic_pointer_cast&lt;ns::c&gt;(bb1)); cs.insert(boost::dynamic_pointer_cast&lt;ns::c&gt;(bb1)); cs.insert(boost::dynamic_pointer_cast&lt;ns::c&gt;(bb2)); cs.insert(boost::dynamic_pointer_cast&lt;ns::c&gt;(bb2)); std::cout &lt;&lt; "This should be 1: " &lt;&lt; cs.size() &lt;&lt; std::endl; } </pre><p> I'm using Xcode 4.2 / gcc on 10.6.8. </p> <p> i686-apple-darwin10-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00) </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/6237 Trac 1.4.3 anonymous Thu, 08 Dec 2011 08:58:47 GMT <link>https://svn.boost.org/trac10/ticket/6237#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6237#comment:1</guid> <description> <p> No, it's valid, you're using a custom <code>operator==</code> so for it to work you need to use a custom hash function. For this case,: </p> <pre class="wiki">bool operator==(const cptr &amp;c1, const cptr &amp;c2) { return true; } // Since cptr is always equal just hash to the same value. std::size_t hash_value(const cptr&amp; c1) { return 1; } </pre><p> The hash function for your real code depends on what your equality operator does. This only worked before because there was a bug for hashing shared_ptr so they all went into the same bucket. This worked but was very inefficient. </p> </description> <category>Ticket</category> </item> <item> <author>Chris Meyer <cmeyer1969+boost@…></author> <pubDate>Mon, 19 Dec 2011 17:28:00 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6237#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6237#comment:2</guid> <description> <p> Thanks for the explanation. This seems like a dangerous and non-obvious pitfall. </p> <p> For reference, the documentation I missed is here: </p> <p> <a href="http://www.boost.org/doc/libs/1_48_0/doc/html/unordered/hash_equality.html">http://www.boost.org/doc/libs/1_48_0/doc/html/unordered/hash_equality.html</a> </p> <p> This bug can be closed now. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Tue, 20 Dec 2011 20:22:51 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/6237#comment:3 https://svn.boost.org/trac10/ticket/6237#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> Ticket