Skip to content

Instantly share code, notes, and snippets.

@octavifs
Last active December 18, 2015 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save octavifs/5728879 to your computer and use it in GitHub Desktop.
Save octavifs/5728879 to your computer and use it in GitHub Desktop.
foreach C++ macro. Takes a variable (will be an iterator. Don't instantiate!) and a container. Salvaged from http://stackoverflow.com/a/197926.
// Usage:
// foreach(it, container) {
// CONTAINER_VALUE_TYPE v = *it
// ...
// }
//
#ifndef FOREACH_MACRO
#define FOREACH_MACRO
#define VAR(V,init) __typeof(init) V=(init)
#define FOREACH(I,C) for(VAR(I,(C).begin());I!=(C).end();I++)
#define foreach(I, C) FOREACH(I,C)
#endif FOREACH_MACRO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment