Boost C++ Libraries: Ticket #6711: with unordered_map using interprocess basic_string in shared memory, find and [] are annoying https://svn.boost.org/trac10/ticket/6711 <p> When using an interprocess basic string such as: </p> <pre class="wiki">typedef boost::interprocess::allocator&lt; char, boost::interprocess::managed_mapped_file::segment_manager &gt; char_allocator_t; typedef boost::interprocess::basic_string&lt; char, std::char_traits&lt;char&gt;, char_allocator_t &gt; key_type; </pre><p> map, flat_map (and unordered_map) all require that one have an externally created temporary of that type (key_type) created that can temporarily hold the results of a std::string or char*. This requires code of the form: </p> <pre class="wiki"> // A temporary string that allocates from the mapped file struct shm_clean { // cleanup shared memory on stack-unwind shm_clean() { boost::interprocess::shared_memory_object::remove("StupidSharedMemory"); } ~shm_clean() { boost::interprocess::shared_memory_object::remove("StupidSharedMemory"); } } cleaner; boost::interprocess::managed_shared_memory stupid(boost::interprocess::create_only ,"StupidSharedMemory" ,500); key_type key_val(stupid.get_segment_manager()); mymap_in_shared_memory[key_val = mystring.c_str()] = junk; </pre><p> This is barely ok when we are working with mutable memory regions, but when I am working in a read-only memory map, it is annoying. </p> <p> Please see this thread: </p> <p> <a class="ext-link" href="http://thread.gmane.org/gmane.comp.lib.boost.devel/228874"><span class="icon">​</span>http://thread.gmane.org/gmane.comp.lib.boost.devel/228874</a> </p> <p> Bug posted at request of Ion Gaztañaga </p> <p> See sister ticked for boost::containers </p> <p> <a class="ext-link" href="https://svn.boost.org/trac/boost/ticket/6710"><span class="icon">​</span>https://svn.boost.org/trac/boost/ticket/6710</a> </p> <p> Joel </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/6711 Trac 1.4.3 Daniel James Wed, 21 Mar 2012 21:31:05 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/6711#comment:1 https://svn.boost.org/trac10/ticket/6711#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> Closing as "won't fix", since boot unordered is an implementation of the standard unordered containers, and this would be a break from the standard. It would also be worse when using <code>std::equal_to</code> or <code>boost::hash</code> since it would have to cast for every call to those function objects rather than just once for the call to <code>operator[]</code>. </p> Ticket olafvdspek@… Wed, 21 Mar 2012 22:04:19 GMT <link>https://svn.boost.org/trac10/ticket/6711#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6711#comment:2</guid> <description> <p> The standard doesn't require you to not have extra find() overloads, does it? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Daniel James</dc:creator> <pubDate>Wed, 21 Mar 2012 23:00:50 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6711#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6711#comment:3</guid> <description> <p> It does, because it can break valid code. Consider this: </p> <pre class="wiki">#include &lt;cassert&gt; #include &lt;functional&gt; struct source {}; struct value { value(source&amp;&amp;) {} }; void foo(value const&amp; x) { std::equal_to&lt;value&gt;(x); } #if defined(ADD_TEMPLATE) template &lt;typename T&gt; void foo(T const&amp; x) { std::equal_to&lt;value&gt;(x); } #endif int main() { foo(source()); } </pre><p> Note that ADD_TEMPLATE adds a template overload, as you're suggesting. Defining ADD_TEMPLATE causes it to fail: </p> <pre class="wiki">$ g++-mp-4.7 --std=c++0x demo.cpp $ g++-mp-4.7 --std=c++0x demo.cpp -DADD_TEMPLATE demo.cpp: In instantiation of 'void foo(const T&amp;) [with T = source]': demo.cpp:19:17: required from here demo.cpp:15:24: error: invalid initialization of reference of type 'const value&amp;' from expression of type 'const source' demo.cpp:9:6: error: in passing argument 1 of 'void f(const value&amp;)' </pre><p> The same could happen if a template overload was added for <code>find</code>. </p> </description> <category>Ticket</category> </item> <item> <author>Olaf van der Spek <olafvdspek@…></author> <pubDate>Fri, 23 Mar 2012 12:35:26 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6711#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6711#comment:4</guid> <description> <p> VC11 says "error C2082: redefinition of formal parameter 'x'" for the first "std::equal_to&lt;value&gt;(x);" </p> <p> Without equal_to(), it compiles fine. </p> <p> Even if it didn't, why not just replace the original find() with a template variant? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 23 Mar 2012 12:58:07 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6711#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6711#comment:5</guid> <description> <p> Sorry, I messed up that example, here's a better one. This one doesn't use rvalue references because I don't have access to an appropriate compiler at the moment, but it demonstrates why a template method would break valid code (<code>foo</code> calls <code>std::equal_to</code> in the same way that <code>find</code> does): </p> <pre class="wiki">#include &lt;functional&gt; struct source {}; struct value { value(source&amp;) {} bool operator==(value const&amp;) const { return true; } }; struct pseudo_container { std::equal_to&lt;value&gt; eq; #if defined(USE_A_TEMPLATE) template &lt;typename T&gt; void foo(T const&amp; x) { eq(x, x); } #else void foo(value const&amp; x) { eq(x, x); } #endif }; int main() { pseudo_container x; source s; x.foo(s); } </pre> </description> <category>Ticket</category> </item> <item> <author>Olaf van der Spek <olafvdspek@…></author> <pubDate>Fri, 23 Mar 2012 13:09:39 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6711#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6711#comment:6</guid> <description> <p> Isn't that because the value constructor takes a non-const &amp; to source? With const&amp; it works. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 23 Mar 2012 13:14:18 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6711#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6711#comment:7</guid> <description> <p> But value types are allowed to use non-const constructors from arbitrary types, I can't stop them doing that. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Fri, 23 Mar 2012 13:14:54 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6711#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6711#comment:8</guid> <description> <p> Sorry, 'are allowed to construct with non-const references from arbitrary types'. </p> </description> <category>Ticket</category> </item> <item> <author>Olaf van der Spek <olafvdspek@…></author> <pubDate>Fri, 23 Mar 2012 13:42:42 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6711#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6711#comment:9</guid> <description> <p> Hmm, equal_to is the real problem, as it requires the two types to be the same. You'd need an equal_to variant that doesn't. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Daniel James</dc:creator> <pubDate>Sun, 25 Mar 2012 21:10:02 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6711#comment:10 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6711#comment:10</guid> <description> <p> Yes, but <code>equal_to</code> is actually a template parameter, whose concept is defined by the standard so I can't change it. </p> </description> <category>Ticket</category> </item> <item> <author>Olaf van der Spek <olafvdspek@…></author> <pubDate>Tue, 27 Mar 2012 11:56:06 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/6711#comment:11 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6711#comment:11</guid> <description> <p> That sucks :p </p> </description> <category>Ticket</category> </item> </channel> </rss>