Firstly, there is no space between the closing bracket at the end of FOR(x,n) and preceding the rest. That's a stylistic concern, but badly hurts readability.

Second, and more significantly, typeof() is not a standard function or operator in C++. It is an extension specific to g++ (GNU c++) in which typeof(x) expands to be type of x and will trigger a compiler error with every compiler that does not support that extension. Within a for loop (assuming you're using g++), if x is an int, then FOR(x,n) would expand to for (int; x < n; x++) which is invalid code.

Apart from anything else this useless macros must be

#define FOR(x,n)for(typeof(x)x=0;x<(n);x++)

"#define FOR(x,n)for(typeof(x)x=0;x<(n);x++)" won't even work. In order for typeof() to function, x has to already exist, and if x already exists the compiler will complain that you're redeclaring x. EG:

int x;

FOR(x,10)
   cout<<"Looping"<<endl;

will expand to "for(int x=0; x<(10);x++", but you already declared x... The closest you might want is "FOR(n) for(int x=0; x<(n); x++)" but I fail to see much point. If you're going to macro a for loop, do so because you're controlling the entire loop for a specific set. Such as:

#define ITERATE_DEVICE_LIST    for(int i=0; i < NUM_DEVICES; i++)

At least this ensures that indexing errors need only be fixed in the macro and prevents stray typos from causing problems in the future.

This article has been dead for over six months. Start a new discussion instead.