Boost C++ Libraries: Ticket #5279: [Foreach] Compile-time const rvalue detection fails with gcc 4.6 https://svn.boost.org/trac10/ticket/5279 <p> Recently, gcc 4.6 changed the behavior of rvalue conversions (from gcc-4.6-20110305). You can see the related info here: <a class="ext-link" href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47851"><span class="icon">​</span>http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47851</a> </p> <p> This breaks compile-time const rvalue detection in Boost.Foreach; Const rvalues are incorrectly treated as lvalues, and so a segmentation fault occurs in the following code: </p> <pre class="wiki">#include &lt;list&gt; #include &lt;boost/foreach.hpp&gt; typedef const std::list&lt;int&gt; clist; int main (int argc, char* argv[]) { BOOST_FOREACH(int x, clist(3)) {} return 0; } </pre><p> Though gcc 4.6 is not yet released, I think this behavior is highly likely to happen in the release version. So I reported this problem here. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5279 Trac 1.4.3 mimomorin@… Tue, 08 Mar 2011 12:22:43 GMT attachment set https://svn.boost.org/trac10/ticket/5279 https://svn.boost.org/trac10/ticket/5279 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">foreach.patch</span> </li> </ul> <p> Disables compile-time const rvalue detection in gcc 4.6 (and newer versions) </p> Ticket Eric Niebler Tue, 08 Mar 2011 12:56:45 GMT status, milestone changed https://svn.boost.org/trac10/ticket/5279#comment:1 https://svn.boost.org/trac10/ticket/5279#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.47.0</span> </li> </ul> <p> I wonder if there is a different hack for gcc 4.6 that lets us detect const-qualified rvalues at compile time. gcc 4.6 supports rvalue references, right? It seems any compiler that supports rvalue references can detect rvalues at compile time. A general fix would be VERY GOOD. </p> Ticket mimomorin@… Tue, 08 Mar 2011 17:05:29 GMT <link>https://svn.boost.org/trac10/ticket/5279#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5279#comment:2</guid> <description> <p> Yeah, with the C++0x features (<code>-std=c++0x</code>), we can easily detect rvalueness. Personally, I use the following code for rvalue detection </p> <pre class="wiki">#ifndef BOOST_NO_DECLTYPE #define BOOST_IS_LVALUE(xxx) boost::is_lvalue_reference&lt;decltype( (xxx) )&gt;::value #define BOOST_IS_RVALUE(xxx) (!BOOST_IS_LVALUE(xxx)) #endif </pre><p> Is it better to develop a method that only needs rvalue reference support (i.e. without using the decltype feature)? </p> </description> <category>Ticket</category> </item> <item> <author>mimomorin@…</author> <pubDate>Wed, 09 Mar 2011 12:57:45 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5279#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5279#comment:3</guid> <description> <p> I will attach a patch which only needs rvalue reference support. I ran the tests locally using gcc 4.6 (w/wo <code>-std=c++0x</code>), and all the tests passed. </p> </description> <category>Ticket</category> </item> <item> <author>mimomorin@…</author> <pubDate>Wed, 09 Mar 2011 13:02:32 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/5279 https://svn.boost.org/trac10/ticket/5279 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">foreach.2.patch</span> </li> </ul> <p> A patch against trunk. Disables compile-time const rvalue detection in gcc 4.6 (and newer versions) and enables it when a compiler supports rvalue references. </p> Ticket Eric Niebler Thu, 10 Mar 2011 01:56:36 GMT <link>https://svn.boost.org/trac10/ticket/5279#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5279#comment:4</guid> <description> <p> Thanks for the patch. I hope to get to this soon. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Eric Niebler</dc:creator> <pubDate>Sun, 13 Mar 2011 14:02:19 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5279#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5279#comment:5</guid> <description> <p> Hrm, for me this fails with gcc 4.5.0 with -std=gnu++0x. Can you confirm? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Eric Niebler</dc:creator> <pubDate>Sun, 13 Mar 2011 14:24:29 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5279#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5279#comment:6</guid> <description> <p> Disregard. There seems to be something wrong with my setup that makes it impossible for me to test with gcc in 0x mode. I'll commit this (modulo some minor changes) and hope for the best. </p> </description> <category>Ticket</category> </item> <item> <author>mimomorin@…</author> <pubDate>Thu, 14 Apr 2011 23:38:32 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5279#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5279#comment:7</guid> <description> <p> Thanks, Eric! The regression test results seem OK. </p> <p> I've also tried to support rvalue reference binding of temporary collections in Boost.Foreach (<code>auto</code> or <code>decltype</code> C++0x feature is needed to extend the lifetime of temporaries). A new ticket and patches for this coming soon. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Eric Niebler</dc:creator> <pubDate>Fri, 15 Apr 2011 03:22:55 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5279#comment:8 https://svn.boost.org/trac10/ticket/5279#comment:8 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> merged to release. thanks for your help with this. </p> Ticket pmachata@… Thu, 21 Jul 2011 21:51:31 GMT <link>https://svn.boost.org/trac10/ticket/5279#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5279#comment:9</guid> <description> <p> Hi there. So for GCC 4.6, foreach will use BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION, which implies that BOOST_FOREACH cannot be used to iterate over noncopyable collections, as noted in the file. Is there a plan to fix this? I'm currently hitting this case when building Wesnoth. Thanks, Petr Machata. </p> </description> <category>Ticket</category> </item> </channel> </rss>