I wanted to create a little macro to simulate for(auto item : container) in VC2010 which I can then replace with the real construct when it's released.
There is BOOST_FOREACH, however I would like auto support.
I've tried creating a macro. However it fails when the dereferenced iterator is a constant type.
#define _LIB_FOREACH_LINENAME_CAT(name, line) name##line
#define _LIB_FOREACH_LINENAME(name, line) _LIB_FOREACH_LINENAME_CAT(name, line)
#define LIB_AUTO_FOREACH(item, expr) \
decltype((expr))&& _LIB_FOREACH_LINENAME(con, __LINE__)((expr)); auto _LIB_FOREACH_LINENAME(it, __LINE__) = _LIB_FOREACH_LINENAME(con, __LINE__).begin(); for(auto (item) = *_LIB_FOREACH_LINENAME(con, __LINE__).begin(); _LIB_FOREACH_LINENAME(it, __LINE__) != _LIB_FOREACH_LINENAME(con, __LINE__).end(); ++_LIB_FOREACH_LINENAME(it, __LINE__), (item) = *_LIB_FOREACH_LINENAME(it, __LINE__))
Anyone up for the challenge to correct mine or find a working implementation?
EDIT:
Notice that (expr) should only be evaluated once.