1

In my C++ .h file:

class foo {
#define useThis true

...
}

In my .cpp file:

#if useThis
... generate A code
#else
... generate B code
#endif

The problem is that the #define values are not being read in the .cpp file, so what is happening is both A and B are being generated.

I am including the .h file in the top of the .cpp file.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Bob Jones
  • 2,049
  • 5
  • 32
  • 60
  • 1
    Firstly, posting real code is going to help more here. There's no way any proper compiler is going to be generating both of those (in this case, `generate B code` will be the winning branch). Secondly, use `1` instead of `true`, as `true` isn't a preprocessor value. That, or just use `#ifdef useThis` or `#if defined(useThis)` and omit the `true` value altogether. – Qix - MONICA WAS MISTREATED Feb 16 '17 at 06:03
  • 1
    [Cannot reproduce.](http://melpon.org/wandbox/permlink/iVXMEjUZLFjJeMSZ) – chris Feb 16 '17 at 06:04
  • 1
    @Qix The C++ preprocessor **does** recognise `true` and `false` as the appropriate boolean values. – Angew is no longer proud of SO Feb 16 '17 at 07:09

1 Answers1

1

Boolean value can not be used in macros for some compilers, like Visual Studio (works under g++ though). A cross compiler way should be:

#define useThis 1

Or, define a macro without value, and use ifdef to test if it has been defined:

#define useThis

#ifdef useThis
    ...
#else
    ...
#endif
zhm
  • 3,513
  • 3
  • 34
  • 55
  • I've gone over this in a now-deleted answer, but [this is incorrect](http://eel.is/c++draft/cpp.cond). *After all replacements due to macro expansion and evaluations of defined-macro-expressions and has-include-expressions have been performed, all remaining identifiers and keywords, except for true and false, are replaced with the pp-number 0, and then each preprocessing token is converted into a token.* and *The resulting tokens comprise the controlling constant expression which is evaluated according to the rules of [expr.const]…* – chris Feb 16 '17 at 14:46
  • In short, `true` is not replaced with 0 like other identifiers are. After that step, the expression `true` is evaluated according to the same rules as constant expression evaluation everywhere else in the language, satisfying the `#if` condition. – chris Feb 16 '17 at 14:48
  • @chris Seems it is a compiler related behavior. Boolean in macro does not work for Visual Studio 2013, but works for g++. – zhm Feb 16 '17 at 15:29
  • I'm not surprised. VS has had incomplete preprocessor support for a long time. I think it will possibly be brought up to speed more toward the end of 2017, or at least after most other standardization issues like two-phase lookup. – chris Feb 16 '17 at 17:10
  • I have tried both #define USE_THIS 1 and testing for #if USE_THIS and #define USE_THIS and testing for #ifdef USE_THIS. In both cases, the tests work in the .h file but not in the .cpp file. – Bob Jones Feb 16 '17 at 19:45
  • @BobJones Just to be sure, did you include your `.h` file in your `.cpp` file? – zhm Feb 16 '17 at 23:46