Boost C++ Libraries: Ticket #3469: Fuller support for maps in Boost.Foreach https://svn.boost.org/trac10/ticket/3469 <p> As documented in the manual (<a href="http://www.boost.org/doc/libs/1_40_0/doc/html/foreach/pitfalls.html">Pitfalls</a>), BOOST_FOREACH is a little awkward to use with associative containers. I propose extending the mechanism, adding a new macro that assigns key and value to separate values. It also generalizes easily to other containers of compile-time-iterable types like Boost.Fusion vectors. </p> <p> The proposed syntax: </p> <pre class="wiki">std::map&lt;int, int&gt; my_map; BOOST_FOREACH_FIELD((int key)(int value), my_map) std::cout &lt;&lt; key &lt;&lt; " : " &lt;&lt; value &lt;&lt; "\n"; std::vector&lt;boost::fusion::vector&lt;int, std::string, int&gt; &gt; my_complex_type; BOOST_FOREACH_FIELD((int i)(std::string const&amp; j)(int&amp; k), my_complex_type) std::cout &lt;&lt; "line: " &lt;&lt; i &lt;&lt; ", " &lt;&lt; j &lt;&lt; ", " &lt;&lt; ++k; </pre><p> As with the current BOOST.FOREACH macro, the variables to assign to can be variables declared before the loop and assigned to at each step, variables scoped in the loop and copy-constructed at each step, or references scoped in the loop and bound at each step, avoiding copying overhead and allowing a mutating loop. </p> <p> Existing workarounds I am aware of: </p> <ul><li>Declare a pair variable before the loop, and assign to it at each iteration. This does not give meaningful names to the two halves of the pair, and has no simple extension to a mutating loop as far as I am aware. </li><li>Typedef the pair type before the loop (no commas in macros), and use a (const) reference in the loop. No copying overhead, but again lacks meaningful names. </li><li>Declare two variables before the loop, and use Boost.Tuple's tie to assign to them at each iteration. This gives meaningful names, but has copying overhead and no simple extension to a mutating loop. </li></ul><p> Attached is an alpha-quality implementation of the idea, without extensive performance or cross-platform testing. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3469 Trac 1.4.3 Chris Purcell <Chris.Purcell.39@…> Sun, 20 Sep 2009 21:12:32 GMT attachment set https://svn.boost.org/trac10/ticket/3469 https://svn.boost.org/trac10/ticket/3469 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">foreach_field.hpp</span> </li> </ul> <p> Alpha-quality implementation of feature </p> Ticket Chris Purcell <Chris.Purcell.39@…> Sun, 20 Sep 2009 21:13:43 GMT summary changed https://svn.boost.org/trac10/ticket/3469#comment:1 https://svn.boost.org/trac10/ticket/3469#comment:1 <ul> <li><strong>summary</strong> <span class="trac-field-old">Fuller support for maps to Boost.Foreach</span> → <span class="trac-field-new">Fuller support for maps in Boost.Foreach</span> </li> </ul> Ticket Eric Niebler Sun, 20 Sep 2009 21:15:07 GMT status changed https://svn.boost.org/trac10/ticket/3469#comment:2 https://svn.boost.org/trac10/ticket/3469#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket Eric Niebler Mon, 14 Dec 2009 02:31:04 GMT milestone changed https://svn.boost.org/trac10/ticket/3469#comment:3 https://svn.boost.org/trac10/ticket/3469#comment:3 <ul> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.41.0</span> → <span class="trac-field-new">Boost 1.42.0</span> </li> </ul> <p> For additional motivation for this addition, see <a class="ext-link" href="http://thread.gmane.org/gmane.comp.lib.boost.user/54162"><span class="icon">​</span>this thread</a>. </p> Ticket cblp@… Tue, 25 Sep 2012 13:31:43 GMT cc set https://svn.boost.org/trac10/ticket/3469#comment:4 https://svn.boost.org/trac10/ticket/3469#comment:4 <ul> <li><strong>cc</strong> <span class="trac-author">cblp@…</span> added </li> </ul> Ticket Eric Niebler Fri, 16 Nov 2012 00:00:56 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/3469#comment:5 https://svn.boost.org/trac10/ticket/3469#comment:5 <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">wontfix</span> </li> </ul> <p> <code>BOOST_FOREACH</code> is now in legacy, bug-fix-only mode. No new features will be added. Users are encouraged to use the new C++11 range-based for loop. </p> Ticket