Boost C++ Libraries: Ticket #5473: [Foreach] non-const rvalue collections are treated as const collections https://svn.boost.org/trac10/ticket/5473 <p> In Boost.Foreach, non-const rvalue collections are treated as const collections. So the following code does not compile: </p> <pre class="wiki">#include &lt;vector&gt; #include &lt;boost/foreach.hpp&gt; int main (int argc, char* argv[]) { BOOST_FOREACH(int&amp; x, std::vector&lt;int&gt;(3)) {} return 0; } </pre><p> I think treating them as non-const collections is more intuitive and, at least, is consistent with C++0x range-based for. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5473 Trac 1.4.3 mimomorin@… Fri, 15 Apr 2011 00:11:36 GMT <link>https://svn.boost.org/trac10/ticket/5473#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5473#comment:1</guid> <description> <p> I made patches for <code>foreach.hpp</code> and <code>libs/foreach/test/</code>, but I cannot attach them since the trac system rejects them as spam... </p> </description> <category>Ticket</category> </item> <item> <author>mimomorin@…</author> <pubDate>Fri, 15 Apr 2011 00:12:36 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/5473 https://svn.boost.org/trac10/ticket/5473 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">test_mutate_rvalue.patch</span> </li> </ul> <p> Add <code>mutate_rvalue.cpp</code> and update Jamfile (a patch for <code>libs/foreach/test/</code> against trunk). </p> Ticket mimomorin@… Fri, 15 Apr 2011 01:13:52 GMT <link>https://svn.boost.org/trac10/ticket/5473#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5473#comment:2</guid> <description> <p> Still fail to attach a patch for <code>foreach.hpp</code>... </p> <p> The patch (will be attached) does the following: </p> <ol><li>Renaming functions </li></ol><ul><li><code>is_const_</code> --&gt; <code>is_nonconst_lvalue_</code> <ul><li>Its return value is also changed. </li></ul></li><li><code>is_rvalue_</code> --&gt; <code>is_nonconst_rvalue_</code> <ul><li>The name of recently added <code>is_rvalue_(T&amp;&amp;, ...)</code> is not changed because it does detect the rvalueness (the ellipsis parameter is no longer needed and was removed). </li></ul></li></ul><ol start="2"><li>Introducing a new macro </li></ol><ul><li>BOOST_FOREACH_IS_CONST <ul><li>This macro returns "true" if the collection is a const lvalue/rvalue. </li></ul></li></ul><ol start="3"><li>Hacking codes for BOOST_FOREACH_RUN_TIME_CONST_RVALUE_DETECTION (this might be an ugly hack...) </li></ol><ul><li>simple_variant <ul><li>To deal with non-const rvalues, non-const <code>get</code> member function is added. This function returns non-const pointer to the (copied) collection. </li><li>Non-const lvalue collections are detected at compile-time, and so the non-const <code>get</code> function is only invoked for rvalues collections. </li></ul></li><li><code>begin</code>/<code>end</code>, <code>rbegin</code>/<code>rend</code> <ul><li>These functions are modified to accept non-const collections. </li><li>Constness of the collection is propagated by means of <code>auto_any_cast</code>. If the collection is const, <code>auto_any_cast</code> returns const <code>simple_variant</code> reference. Otherwise, it returns non-const <code>simple_variant</code> reference. </li></ul></li></ul><p> All the tests (+ <code>mutate_rvalue.cpp</code>) ran successfully with the following compilers (both in C++03 and C++0x modes): gcc-4.3.5, gcc-4.4.5, gcc-4.5.2, gcc-4.6.0, clang (TOT). </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Eric Niebler</dc:creator> <pubDate>Fri, 15 Apr 2011 02:10:50 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5473#comment:3 https://svn.boost.org/trac10/ticket/5473#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">wontfix</span> </li> </ul> <p> Please don't spend any more time on this. Mutable iteration over an rvalue collection makes no sense. I consider the fact that BOOST_FOREACH disallows it a feature. I'm surprised C++0x range-for allows it, but I don't that that's reason enough for BOOST_FOREACH to support it. </p> Ticket mimomorin@… Fri, 15 Apr 2011 02:38:26 GMT <link>https://svn.boost.org/trac10/ticket/5473#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5473#comment:4</guid> <description> <p> OK, I agreed with you that mutable iteration over rvalue ranges is non-sense. </p> </description> <category>Ticket</category> </item> <item> <author>bernd.loerwald@…</author> <pubDate>Mon, 05 Nov 2012 13:03:58 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5473#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5473#comment:5</guid> <description> <p> It actually is not non-sense. </p> <p> Given a class being a facade container to an actual container, one might construct the facade container on the fly: </p> <blockquote> <p> values_type values() const { return values_type (*this); } </p> </blockquote> <p> and use it in a manipulating way: </p> <blockquote> <p> BOOST_FOREACH (elem_type&amp; elem, values()) </p> </blockquote> <p> with values_type having non-const iterators, that iterate over the actual container (which is referenced in values_type). </p> <p> This will fail, as values() returns an rvalue, thus only const iteration is possible. It would be perfectly valid to modify the elements iterated over with the facade. </p> </description> <category>Ticket</category> </item> </channel> </rss>