Foreach in C++ stl

#define CONCAT_IMPL( x, y ) x##y
#define MACRO_CONCAT( x, y ) CONCAT_IMPL( x, y )
#define PPUniVar(name) MACRO_CONCAT(name, __COUNTER__)

#define _foreach(var, collection, collection_to_iter, collection_type) \
typedef typeof(collection) collection_type; \
collection_type collection_to_iter = (collection); \
collection_type::iterator _##var=collection_to_iter.begin(); \
if (_##var!=collection_to_iter.end()) \
for (collection_type::value_type var = *_##var; _##var!=collection_to_iter.end() && (var = *_##var, true); _##var++ )

#define foreach(var, collection) \
_foreach(var, collection, PPUniVar(collection_to_iter), PPUniVar(collection_type))

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: