Boost C++ Libraries: Ticket #11850: templated overload of boost::lockfree::spsc_queue::pop is a potential footgun https://svn.boost.org/trac10/ticket/11850 <p> boost::lockfree::spsc_queue::pop has many overloads. I've seen code that to tries to select this one: </p> <pre class="wiki">template&lt;typename U&gt; boost::enable_if&lt; typename is_convertible&lt; T, U &gt;::type, bool &gt;::type pop(U &amp; ret); </pre><p> but, by mistake, selects this one: </p> <pre class="wiki">template&lt;typename OutputIterator&gt; boost::disable_if&lt; typename is_convertible&lt; T, OutputIterator &gt;::type, size_type &gt;::type pop(OutputIterator it); </pre><p> How? The problematic code looks like this: </p> <pre class="wiki">boost::spsc_queue&lt;Thing&gt; q; void consumer() { for (;;) { while (!q.empty()) { OtherThing foo = ... Thing bar; AnotherThing baz = ...; q.pop(&amp;bar); // do something with foo, bar, and baz } } } </pre><p> Note the &amp; operator, which will select the templated "pop to an output iterator" overload because pointers to non-const are output iterators, rather than lead to a compilation error (as one might expect). This is a nasty bug because when there isn't much pressure on the queue, and at most one element is present, the consumer will appear to work correctly. But if there is more than one element in the queue, popping all of them off will cause undefined behavior (probably overwriting the memory of foo or baz in the above example) that, speaking from experience, is pretty hard to nail down. </p> <p> My request is to deprecate this templated "pop to an iterator" overload, and make a new member function called pop_all with the same signature to replace it. This would help to prevent this mistake, and also make the API somewhat more consistent with the consume_one/consume_all functions. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/11850 Trac 1.4.3 tjakubowski@… Thu, 17 Dec 2015 03:13:32 GMT <link>https://svn.boost.org/trac10/ticket/11850#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/11850#comment:1</guid> <description> <p> Replying to <a class="new ticket" href="https://svn.boost.org/trac10/ticket/11850" title="#11850: Feature Requests: templated overload of boost::lockfree::spsc_queue::pop is a potential ... (new)">tjakubowski@…</a>: </p> <blockquote class="citation"> <pre class="wiki">boost::spsc_queue&lt;Thing&gt; q; </pre></blockquote> <p> Oops, I meant to say boost::lockfree::spsc_queue here, of course. </p> </description> <category>Ticket</category> </item> </channel> </rss>