4 ms·
The hygienic C macro is broken. It breaks if your variable happens to already be named "tmp": swap(x, tmp). This would work however: #define SWAP(x, y) {
by tolmasky 12y ago
The hygienic C macro is broken. It breaks if your variable happens to already be named "tmp": swap(x, tmp). This would work however:
#define SWAP(x, y) { \
typeof (x) x##y = x; \
x = y; \
y = x##y; \
}
Also, you can be tricky and "co-recurse" in macros, which is explained here: https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,...
Regarding each-it, I think its pretty doable in C. Right off the top of my head, this seems to work:
#define eachIt(myArray) typeof(myArray[0]) it; for (int i = 0; i < sizeof(myArray)/sizeof(myArray[0]) && (it = myArray[i], 1); ++i)
This can now be used just as in the example:
struct test_ { int one; double two; };
typedef test_ test;
test array[17];
eachIt(array) {
it.one = 1;
it.two = 0.2;
}
However, I believe(?) that it depends on the version of the C standard whether this is hygienic or not. When I compile in GCC it certainly works fine because the i is counted as internal to the for loops scope, but I'm not sure that's always the case. There is also the separate question of whether "it" should be hygienic or not.