Subject: [Boost-bugs] [Boost C++ Libraries] #3469: Fuller support for maps to Boost.Foreach
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2009-09-20 21:10:48
#3469: Fuller support for maps to Boost.Foreach
--------------------------------------------------------+-------------------
Reporter: Chris Purcell <Chris.Purcell.39@â¦> | Owner: eric_niebler
Type: Feature Requests | Status: new
Milestone: Boost 1.41.0 | Component: foreach
Version: Boost 1.40.0 | Severity: Not Applicable
Keywords: |
--------------------------------------------------------+-------------------
As documented in the manual
([http://www.boost.org/doc/libs/1_40_0/doc/html/foreach/pitfalls.html
"Pitfalls"]), 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.
The proposed syntax:
{{{
std::map<int, int> my_map;
BOOST_FOREACH_FIELD((int key)(int value), my_map)
std::cout << key << " : " << value << "\n";
std::vector<boost::fusion::vector<int, std::string, int> >
my_complex_type;
BOOST_FOREACH_FIELD((int i)(std::string const& j)(int& k),
my_complex_type)
std::cout << "line: " << i << ", " << j << ", " << ++k;
}}}
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.
Existing workarounds I am aware of:
* 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.
* 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.
* 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.
Attached is an alpha-quality implementation of the idea, without extensive
performance or cross-platform testing.
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/3469> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:01 UTC