0

If I have a simple statement like this:

#ifndef A(B)
#define A(B) B
#endif

I get a compilation error...

error: extra tokens at end of #ifndef directive [-Werror]
 #ifndef A(B)
          ^

But if I write (say):

#ifndef A
#define A(B) B
#endif

I have no problem - why is it possible to define this macro but it's not possible to check if it's not defined? That seems somewhat odd.

adrianmcmenamin
  • 1,081
  • 1
  • 15
  • 44
  • "why is it possible to define this macro but it's not possible to check if it's not defined?" But as you demonstrate, `#ifndef A` checks is `A` is defined. Since you're asking the question I assume you're trying to ask a different question; is it, "how do you detect that a macro is a function like macro" perchance? – H Walters Jun 27 '20 at 19:51
  • Clearly the token A is not the same as the token A(B) - so I'm not sure what your point is? I was merely demonstrating that the #ifndef/#define/#endif structure worked in general. – adrianmcmenamin Jun 27 '20 at 21:32
  • `A(B)` isn't a token; it's four tokens. `#define A(B) B` defines a macro called `A`, which is function-like with a single parameter `B`, and a replacement list `B`. There can only be one macro called `A` at any given time, be it object-like, or function-like with any number of parameters. `#ifdef A` checks if a macro called `A` is defined. Is your question why `#ifdef A(B)` doesn't work then? – H Walters Jun 28 '20 at 05:50

0 Answers0