2

If I have:

#define A 1
#define B() A
#undef A

This code:

#if B()
    std::cout << "B" << std::endl;
#else
    std::cout << "not B" << std::endl;
#endif

will print "not B". Is there a way to assign A to another macro variable/function and then wipe out A?

Background: Using this in the context of cmake's configure_file, in which A is defined with a #cmakedefine01.

Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197
  • Macro substitution is made after A undef. You can't do that. BTW why do you need to get rid of that define? – LPs Mar 14 '16 at 10:58
  • @LPs I want to make sure people use `#if B()` and not `#ifdef A` or `#if A`. On that point of view, having an `#undef A` should be more self-documenting – Antonio Mar 14 '16 at 11:16
  • As I wrote, B will be substituite with its content after undef A. The the resulting code will be always "not B". AFAIK you can't do that with defines and macro: "nun se po fa" ;) – LPs Mar 14 '16 at 11:29
  • @Antonio: Just document it. You're using macros, not classes - you can't "encapsulate" them. – Lightness Races in Orbit Mar 14 '16 at 11:37

1 Answers1

3

No.

Macros are not classes and do not have that level of sophistication.

Where one macro is defined in terms of another, both must remain in existence for the substitution to work properly.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055