Recently I have found and I have started using in my preprocessor the following code:
#define FOREACH(i,s) for(VAR(i,(s).begin()); i != (s).end(); i++)
#define VAR(a,b) __typeof(b) a=(b)
what makes my iterating easier. But unfortunately I don’t fully understand the second line, especially the __typeof keyword (and why those two underscores are used). I also assumed that the whole expression __typeof(b) is a type casting, but when I take it in parenthesis, why it does not work?
Just assume some real values for i and s to see what it does:
This will resolve in the macro
FOREACH(i, list):Now resolve macro
VAR(i, (list).begin()):Where __typeof gets the type of the Argument
(list).begin()which is in this casestd::list<int>::iteratorNow insert this into the for and get:
As you see the __typeof part is no typecast but a declaration, so the paranthesis are wrong there.
Also note the many comments on why not to use macros and __typeof in special!