#include <iostream>
#define MY_CONST 10
#define MY_OTHER_CONST MY_CONST
#undef MY_CONST
int main() {
enum my_enum : int {
MY_CONST = 100
};
std::cout << MY_OTHER_CONST;
return 0;
}
I would expect 10 as an output, but this program outputs 100. Can someone explain what is going on here?
#define MY_OTHER_CONST MY_CONSTdefines the macroMY_OTHER_CONSTto have a replacement list ofMY_CONST. No replacement is performed when defining a macro.In
std::cout << MY_OTHER_CONST;,MY_OTHER_CONSTis replaced by its replacement list, becomingMY_CONST. At this point, there is no macro definition forMY_CONST, so no further replacement is performed. ThenMY_CONSTrefers to theenumconstantMY_CONST, which has value 100.