Boost C++ Libraries: Ticket #11994: Support intrusive container key extractors that return the key by value https://svn.boost.org/trac10/ticket/11994 <p> <a href="http://www.boost.org/doc/libs/1_60_0/doc/html/intrusive/map_multimap.html">Map and multimap-like interface for associative containers</a> specifies that the "key extractor" passed by <code>key_of_value</code> must provide a member function to obtain a const reference to the key stored inside a <code>value_type</code>. </p> <p> According to my experience, if the key extractor returns the key by value instead of const reference, then the code compiles but the program crashes a run time. This is a real pitfall. </p> <p> Consider supporting key extraction by value as well. It would allow to use keys that are not stored directly in the value_type, or not directly accessible by reference. </p> <p> For example: </p> <div class="wiki-code"><div class="code"><pre> <span class="k">using</span> <span class="n">boost</span><span class="o">::</span><span class="n">intrusive</span><span class="p">;</span> <span class="k">struct</span> <span class="nl">my_node</span><span class="p">:</span> <span class="n">set_base_hook</span><span class="o">&lt;&gt;</span> <span class="p">{</span> <span class="n">string</span> <span class="n">s</span><span class="p">;</span> <span class="p">};</span> <span class="c1">// The key is the length of my_node::s.</span> <span class="k">struct</span> <span class="n">by_length</span> <span class="p">{</span> <span class="k">using</span> <span class="n">key_type</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="kt">size_t</span><span class="p">;</span> <span class="c1">// Return key by value.</span> <span class="n">std</span><span class="o">::</span><span class="kt">size_t</span> <span class="k">operator</span><span class="p">()(</span><span class="k">const</span> <span class="n">my_node</span><span class="o">&amp;</span> <span class="n">n</span><span class="p">)</span> <span class="k">const</span> <span class="p">{</span> <span class="k">return</span> <span class="n">n</span><span class="p">.</span><span class="n">s</span><span class="p">.</span><span class="n">length</span><span class="p">();</span> <span class="p">}</span> <span class="p">};</span> <span class="n">multiset</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="p">,</span> <span class="n">key_of_value</span><span class="o">&lt;</span><span class="n">by_length</span><span class="o">&gt;&gt;</span> <span class="n">v</span><span class="p">;</span> <span class="n">v</span><span class="p">.</span><span class="n">insert</span><span class="p">(...);</span> <span class="c1">// Find a string by length.</span> <span class="k">const</span> <span class="k">auto</span> <span class="n">it</span> <span class="o">=</span> <span class="n">v</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="mi">3</span><span class="p">);</span> <span class="p">...</span> </pre></div></div><p> If this is not possible, then add a static assertion to make sure that the code doesn't compile at all. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11994 Trac 1.4.3 Ion Gaztañaga Fri, 26 Feb 2016 18:56:50 GMT <link>https://svn.boost.org/trac10/ticket/11994#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11994#comment:1</guid> <description> <p> Returning keys by value sounds interesting, I'm not sure about the impact, I'll investigate it. Thanks for the proposal. </p> </description> <category>Ticket</category> </item> <item> <author>martin.seemann@…</author> <pubDate>Fri, 15 Apr 2016 12:32:42 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/11994#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11994#comment:2</guid> <description> <p> Btw.: The functor needs a 'type' member instead of 'key_type'. It would be nice to fix this in the documentation. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Mon, 04 Jul 2016 20:14:54 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/11994#comment:3 https://svn.boost.org/trac10/ticket/11994#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">fixed</span> </li> </ul> <p> Thanks for the report and patience. Fixed in develop, commit: </p> <p> <a class="ext-link" href="https://github.com/boostorg/intrusive/commit/4014562502d7a9fb9a13b09a908e0d757e368969"><span class="icon">​</span>https://github.com/boostorg/intrusive/commit/4014562502d7a9fb9a13b09a908e0d757e368969</a> </p> Ticket fdegros@… Tue, 05 Jul 2016 07:42:28 GMT <link>https://svn.boost.org/trac10/ticket/11994#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11994#comment:4</guid> <description> <p> Excellent. Thanks for fixing it. </p> </description> <category>Ticket</category> </item> </channel> </rss>