Boost C++ Libraries: Ticket #10678: search by a compatible key and default predicates https://svn.boost.org/trac10/ticket/10678 <p> Ordered and hashed indexes require predicates for comparing elements, which default to std::equal_to&lt;Key&gt; and std::less&lt;Key&gt; respectively. </p> <p> At the same time find functions take a compatible key, that does not need to be convertible to Key. </p> <p> Using std::equal_to&lt;Key&gt; and std::less&lt;Key&gt; breaks find when the compatible key is not convertible to Key because these predicates require both arguments to be of type Key. </p> <p> Something like C++14 std::equal_to&lt;void&gt; and std::less&lt;void&gt; should be used as the default predicates. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/10678 Trac 1.4.3 Joaquín M López Muñoz Fri, 05 Dec 2014 10:45:56 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/10678#comment:1 https://svn.boost.org/trac10/ticket/10678#comment:1 <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">wontfix</span> </li> </ul> <p> Hi Maxim, </p> <p> <code>std::equal_to&lt;void&gt;</code>: in order to deal with compatible keys for hashed indices, you'd need not only transparent equality comparison but also some sort of transparent hash functor such as </p> <pre class="wiki">struct generic_hash { template&lt;typename T&gt; std::size_t operator()(const T&amp; x)const { boost::hash&lt;T&gt; h; return h(x); } }; </pre><p> but using this is tricky (and dangerous): </p> <pre class="wiki">multi_index_container&lt; std::string, indexed_by&lt; hashed_unique&lt;identity&lt;std::string&gt;,generic_hash,std::less&lt;void&gt;&gt; &gt; &gt; c{"hello"}; std::cout&lt;&lt;*(c.find("hello"))&lt;&lt;"\n"; // crash </pre><p> The reason for the problem is: hashing a <code>std::string</code> does not yield the same value has hashing a <code>const char*</code>, so <code>c.find("hello")</code> does not find the string "hello". This is why <a class="ext-link" href="http://open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3657.htm"><span class="icon">​</span>N3657</a> applies only to associative containers and has not been extended to unordered associative containers. </p> <p> As for <code>std::less&lt;void&gt;</code>, I'm sympathetic to your proposal but would prefer to go in line with the standard, which decided for <code>std::less&lt;void&gt;</code> to be explicitly provided by the user rather than the default. </p> Ticket bugs@… Mon, 08 Dec 2014 10:19:47 GMT <link>https://svn.boost.org/trac10/ticket/10678#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10678#comment:2</guid> <description> <p> @joaquin </p> <p> Thanks for the insightful explanations. </p> <p> I love boost multi index containers and I'm continuously surprised just how much effort went into not creating arbitrary limitations. </p> </description> <category>Ticket</category> </item> </channel> </rss>