(VARIABLE) not rated correctly MSVC?

Running the following code

#include <iostream> #define FOO #define BAR defined(FOO) int main() { #if BAR std::cout << "BAR enabled!" << std::endl; #else std::cout << "BAR disabled!" << std::endl; #endif return 0; } 

in Visual Studio displays Bar disabled! , running the same code in gcc or clang displays Bar enabled! .

Is this a bug in the Microsoft compiler? What is the standard?

+6
source share
1 answer

This behavior is undefined according to the standard.

[cpp.cond] , my accent

Before evaluation, the macro commands in the list of preprocessing tokens are replaced, which will become the control constant expression (with the exception of those macro names that have been changed by the unary operator defined ), as in plain text. If the defined token is created as a result of this replacement process or the use of the defined unary operator does not correspond to one of the two specified forms before replacing the macro, the behavior is undefined .

+7
source

All Articles