Foreach

  • Implements: n2930 "Range-Based for Loop wording"
  • Header: cxxomfort/library.hpp
  • Macro: If emulation is enabled, CXXOMFORT_USING_FOREACH_EMULATION is defined.

This library adds a rudimentary 'foreach' construct based off of the well-known Artima article on the subject. The construct is called via the macro CXXO_FOREACH(variabledecl, rangeexpr). This foreach implementation is lifted from the Artima original article from 2003, so it does not have Boost.Foreach's added 20 MB of dependencies. Instead, it relies on container-like types adequately propagating the constness of their iterator types.

Usage is as follows:

vector<int> container;
...
CXXXO_FOREACH (int decl, container) {
    // here you have read-only visitation
    ....
}

CXXXO_FOREACH (int& ref, container) { // here you have writeable visitation .... }

In C++11, this simply expands to for (variabledecl : rangeexpr) supported by the language, for zero compile-time or run-time overhead.