4 ms·
The parent comment explained pretty well the advantages over __LINE__ imo. Crafting macros is often black magic, but using them shouldn't be (if they're well c
by scratcheee 4y ago
The parent comment explained pretty well the advantages over __LINE__ imo.
Crafting macros is often black magic, but using them shouldn't be (if they're well crafted). Having an implicit rule in your macro that it cannot be used twice in the same line is surprising and potentially dangerous.
Another example is where you have a macro doing lots of work, if it needs to use a submacro multiple times that itself needs a unique identifier, then __LINE__ is no longer sufficient.
__UEXP_COUNTER__ is a little more difficult to imagine a use-case for, I'll admit (I can see it allows passing counters around, but I can't see why a parameter couldn't do the same).
Again, preprocessor macros are black magic, these additions seem a lot simpler to understand than `__VA_OPT__` (and its predecessor `##__VA_ARGS__`) or MSVCs awful stringify problems, as you brought up.
- kazinator 4y ago__UEXP_COUNTER__ is required so that you can abstract creating unique symbols: #define MAC(...) WHATEVER UNIQ(X) WHATEVER without __UEXP_COUNTER__, you would have to pass __EXP_COUNTER__ as an extra argument to UNIQ(X). You cannot make a nickname for __EXP_COUNTER__; e.g. #define EC __EXP_COUNTER__ because the expansion of EC itself has its own counter! Using __UEXP_COUNTER__, UNIQ(X) can just do everything necessary: obtain MAC's counter, and combine it with the prefix X.