c++ - Does order matter when #defines are using other #defines? -
according answer this question, following code legit:
#define 3 three #define 9 three*3 int main() { std::cout << nine; return 0; } and sure, compiles , runs fine. however, answer mentioned question states 1 should careful order of such #define directives, , 1 used in other #defines should defined before them. following code:
#define 9 three*3 #define 3 three int main() { std::cout << nine; return 0; } also compiles , runs fine, , prints "9".
is compiler letting me off easy, or order indeed not matter #defines use other #defines? compilation fail on more complicated project?
one thing worth mentioning mentioned question speaks of c, whilst code in c++. (supposed) differences in behavior come from?
three macro need defined before use of nine macro. can change three before every use of nine:
#define 9 three*3 #define 3 three int main() { std::cout << nine; //9 #undef 3 #define 3 4 std::cout << nine; //12 #undef 3 //no `three` macro defined here int 3 = 2; std::cout << nine; //three * 3 == 6 return 0; }
Comments
Post a Comment