0

There is an opinion that it is impossible to write a general purpose emptiness detector macro:

/*
 * Expands to token 1 if the argument list is empty.
 * Expands to token 0 if the argument list is not empty (excluding comments).
 */
#define IS_EMPTY(...)       /* ??? */

However, some "close-to" implementations do exist.

Questions:

  1. Can someone provide a formal proof or at least a correct argumentation proving / convincing that it is impossible to implement the general purpose emptiness detector macro?
  2. Since some "close-to" implementations do exist, the question is: how close are we to the boundary at which "close-to" implementation becomes "general" implementation?

Note: One opinion is that we are awfully close. But how awfully exactly? How we can estimate this (numerically or using other metrics / indicators)?

Mat
  • 202,337
  • 40
  • 393
  • 406
pmor
  • 5,392
  • 4
  • 17
  • 36
  • 2
    Is this a theoretical question or you have some application in mind? – Eugene Sh. Nov 27 '20 at 22:23
  • If what you're saying is true, I'd suggest that the reason is that you can do this up to a pre-specified number of arguments. – klutt Nov 27 '20 at 22:35
  • 1
    It can be done with the restriction that the arguments not start with a left parentheses using the definitions in [this answer](https://stackoverflow.com/questions/53297695/implementing-formatted-print-with-the-possibility-to-do-nothing-when-it-gets-no/53312059#53312059). Change `PRINT` to `IS_EMPTY`, `printf(__VA_ARGS__);` to `1`, and `Argument2(a` to `Argument2(a 0`. – Eric Postpischil Nov 27 '20 at 23:53
  • C11 [§6.10.3 Macro replacement ¶4](http://port70.net/~nsz/c/c11/n1570.html#6.10.3p4) says: _If the identifier-list in the macro definition does not end with an ellipsis, the number of arguments (including those arguments consisting of no preprocessing tokens) in an invocation of a function-like macro shall equal the number of parameters in the macro definition. Otherwise, there shall be more arguments in the invocation than there are parameters in the macro definition (excluding the ...)._ The second sentence means that at least one argument must be provided to the proposed `IS_EMPTY()` macro. – Jonathan Leffler Nov 28 '20 at 05:51
  • 1
    @JonathanLeffler: I think for these purposes, empty `()` count as one argument with no tokens, just as `(,)` counts as two arguments, each with no tokens. Besides accepting `foo()` for `#define foo(...)`, Clang accepts it for `#define foo(x)` and, in the replacement list, replaces `x` with an empty sequence of tokens. GCC does too. MSVC complains for `#define foo(x)` but not for `#define foo(...)`, but that is inconsistent, so I would not trust MSVC’s interpretation or implementation of the standard. – Eric Postpischil Nov 28 '20 at 13:41
  • @EricPostpischil: Thanks! Tests show that it should be the inverse: change `printf(__VA_ARGS__);` to `0` and change `Argument2(a` to `Argument2(a 1`. Can you check? – pmor Nov 28 '20 at 21:43
  • @pmor: Yes, I had the results reversed. – Eric Postpischil Nov 28 '20 at 21:49
  • @EricPostpischil For `#define X(a)` `IS_EMPTY(X)` evaluates to `0`, but expected is `1`, because `the argument list is empty`. – pmor Nov 28 '20 at 22:08

0 Answers0