It replaces for with for(int z=0;z<2;++z)for . Obviously this would turn
for (int i = 0; i < N; ++i) { // ... }
in
for (int z=0;z<2;++z) for (int i = 0; i < N; ++i) { // ... }
Thus, two nested loops are created. Without this extra for it will be
for (int z=0;z<2;++z) (int i = 0; i < N; ++i) { // ... }
This is clearly wrong.
Please note that even if this is “correct” in the form that you gave in your question, this does not mean that it is “good practice”. This is an example of excessive abuse of macros and should be avoided. Here is one of many examples of how this might go wrong:
for (int z = 0; z < 5; ++z) { for (int i = 0; i < 3; ++i) { std::cout << z << std::endl; // this will never print 2, 3, 4 } }
It will expand to
for (int z=0;z<2;++z) for (int z = 0; z < 5; ++z) { for (int z=0;z<2;++z) for (int i = 0; i < 3; ++i) { std::cout << z << std::endl; // this will never print 2, 3, 4 } }
We believe that now you have four nested loops, and that the inner loop will print the “invisible” z instead of z , which you declared in the outer loop (which becomes a second-level loop in the extended code).
Another reason: as @stefan pointed out, it is a very bad idea to use keywords or other well-known identifiers as macro names. Makes you think of the infamous #define true false . And, as @HolyBlackCat mentions, this is also undefined behavior, which means that anything can happen with respect to the standard. Because of the code that “seems to work” for the full-blown World War III with the Martians (who invaded Earth to clear it of the ugly code).
source share