Opened 8 years ago

Closed 5 years ago

#3469 closed Feature Requests (wontfix)

Fuller support for maps in Boost.Foreach

Reported by: Chris Purcell <Chris.Purcell.39@…> Owned by: eric_niebler
Milestone: Boost 1.42.0 Component: foreach
Version: Boost 1.40.0 Severity: Not Applicable
Keywords: Cc: cblp@…


As documented in the manual (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.

Attachments (1)

foreach_field.hpp (1.9 KB) - added by Chris Purcell <Chris.Purcell.39@…> 8 years ago.
Alpha-quality implementation of feature

Download all attachments as: .zip

Change History (6)

Changed 8 years ago by Chris Purcell <Chris.Purcell.39@…>

Alpha-quality implementation of feature

comment:1 Changed 8 years ago by Chris Purcell <Chris.Purcell.39@…>

  • Summary changed from Fuller support for maps to Boost.Foreach to Fuller support for maps in Boost.Foreach

comment:2 Changed 8 years ago by eric_niebler

  • Status changed from new to assigned

comment:3 Changed 8 years ago by eric_niebler

  • Milestone changed from Boost 1.41.0 to Boost 1.42.0

For additional motivation for this addition, see this thread.

comment:4 Changed 5 years ago by cblp@…

  • Cc cblp@… added

comment:5 Changed 5 years ago by eric_niebler

  • Resolution set to wontfix
  • Status changed from assigned to closed

BOOST_FOREACH 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.

Add Comment

Modify Ticket

Change Properties
Set your email in Preferences
as closed The owner will remain eric_niebler.
The resolution will be deleted. Next status will be 'reopened'.

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.