I have a custom c++ macro to emulate a foreach loop:
#define foreach(TYPE, ELEMENT, COLLECTION_TYPE, COLLECTION)\
for(COLLECTION_TYPE::iterator ELEMENT##__MACRO_TEMP_IT = COLLECTION.begin(); ELEMENT##__MACRO_TEMP_IT != COLLECTION.end(); ELEMENT##__MACRO_TEMP_IT++) \
{ TYPE ELEMENT = *(ELEMENT##__MACRO_TEMP_IT);
I know there are alternative ways to do a foreach loop - either by using C++11's, STL's, Qt's or Boost's foreach, but I was attempting a custom solution just for experience's sake.
The problem is, this macro requires either ending with 2 braces ("}}") or omit first brace and end with one, like this:
foreach(int, i, std::list<int>, indexes)
{
//do stuff
}}
or
foreach(int, i, std::list<int>, indexes)
//do stuff
}
I was wondering: is there a smart macro workaround to this, so one can use this macro as follows?
foreach(int, i, std::list<int>, indexes)
{
//do stuff
}