In C++20, is a macro considered "active" if it's #undef'd, then #define'd again?
Let's see if I can lawyer that.
The paragraphs above where the link points say:
- Each #define directive encountered when preprocessing each translation unit in a program results in a distinct macro definition.
So, #define VER 1
is a definition, and #define VER 2
is a distinct one.
5.1 The point of definition of a macro definition within a translation unit is the point at which its #define directive occurs
Both have a point of definition, obviously.
5.2 The point of undefinition of a macro definition within a translation unit is the first point at which a #undef directive naming the macro occurs after its point of definition, [...]
And #define VER 1
has a point of undefinition, while #define VER 2
doesn't.
Therefore, the macro definition of #define VER 2
is active at the location of the test. At some earlier points, #define VER 1
would be active instead.
Then again, if you were to do this:
#define X 1
#define X 2
#undef X
/* is X active now ??? */
There wouldn't seem to be a "point of undefinition" for the first #define
, but I think you'd run afoul of
7 If a macro would be replaced or redefined, and multiple macro definitions are active for that macro name, the active macro definitions shall all be valid redefinitions of the same macro
because they're not the same macro. (There's examples in the cpp.replace page.) Though GCC and Clang accept that with a warning, with the obvious semantics of redefining it with the new value (and not e.g. stacking the definitions so that #undef
would only remove one -- that way lies madness.)