0
constexpr bool bFooOrBar = true;
void test()
{

#if (bFooOrBar ==true)
    cout << "foo" << endl;
#else
    cout << "bar" << endl;
#endif
}

It wouldn't work for expecting output foo. I know it works for an ordinary if else statement. But why not for the precompile macro #if?

Zhang
  • 3,030
  • 2
  • 14
  • 31
  • The pre-processor (i.e. the program that evaluates #if directives) is executed before the compiler and therefore has no knowledge of the remainder of the code. You could use the pre-processor to process any type of file and not just C/C++ and it would work fine because it only looks at the #-directives and nothing else. – Lehks Mar 30 '22 at 04:45
  • @Lehks, That last sentence isn't quite true. For starters, the preprocessor operates on preprocessing tokens that are defined by C and C++. Even if it were to only look at lines starting with `#`, it would miss line splices and block comments in normal C and C++ code. – chris Mar 30 '22 at 05:00

0 Answers0