Boost C++ Libraries of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards


History and Acknowledgements


The ideas for BOOST_FOREACH began life in the Visual C++ group at Microsoft during the early phases of the design for C++/CLI. Whether to add a dedicated "foreach" looping construct to the language was an open question at the time. As a mental exercise, Anson Tsao sent around some proof-of-concept code which demonstrated that a pure library solution might be possible. The code was written in the proposed C++/CLI dialect of the time, for which there was no compiler as of yet. I was intrigued by the possibility, and I ported his code to Managed C++ and got it working. We worked together to refine the idea and eventually published an article about it in the November 2003 issue of the CUJ.

After leaving Microsoft, I revisited the idea of a looping construct. I reimplemented the macro from scratch in standard C++, corrected some shortcomings of the CUJ version and rechristened it BOOST_FOREACH. In October of 2003 I began a discussion about it on the Boost developers list, where it met with a luke-warm reception. I dropped the issue until December 2004, when I reimplemented BOOST_FOREACH yet again. The new version only evaluated its sequence expression once and correctly handled both lvalue and rvalue sequence expressions. It was built on top of the recently accepted Boost.Range library, which increased its portability. This was the version that, on Dec. 12 2004, I finally submitted to Boost for review. It was accepted into Boost on May 5, 2005.


Thanks go out to Anson Tsao of Microsoft for coming up with the idea and demonstrating its feasibility. I would also like to thank Thorsten Ottosen for the Boost.Range library, on which the current version of BOOST_FOREACH is built. Finally, I'd like to thank Russell Hind, Alisdair Meredith and Stefan Slapeta for their help porting to various compilers.

Further Reading

For more information about how BOOST_FOREACH works, you may refer to the article Conditional Love at The C++ Source.