Too early evaluation of macro name ?

Started by aMarCruz, April 19, 2014, 11:51:13 PM

Previous topic - Next topic

aMarCruz

Hi Pelle,
thanks for your excellent work.
Does the behavior of this code is correct?


#define TRIGGER_ZERO(...) 0
#define TEST_ZERO_CONCAT(...)  TRIGGER_ZERO ##__VA_ARGS__()
#define TEST_ZERO_DIRECT(...)  TRIGGER_ZERO __VA_ARGS__()

int _cdecl main(void) {
    int i = 0;
    i = TEST_ZERO_CONCAT();                  //Ok, emit 0 with simple/no arguments
    i = TEST_ZERO_CONCAT((void),foo);   //emit '0,foo()' but expected warning #1058
    i = TEST_ZERO_DIRECT();                   //emit literal "TEST_ZERO_DIRECT()" and obvious linkage error
    return 0;
}


I think this is a bug, TEST_ZERO_DIRECT() should generate 0, as do Clang and MinGW.
Thanks.

Pelle

/Pelle

aMarCruz

#2
Hi Pelle.
mmm... maybe not explained well (sorry, english is not my language).

The way is not TRIGGER_ZERO ##__VA_ARGS__(),  but TRIGGER_ZERO __VA_ARGS__()

In the example above, the line with
i = TEST_ZERO_DIRECT();

"TEST_ZERO_DIRECT()" should be replaced and evaluated as "TRIGGER_ZERO ()" (because __VA_ARGS__ is blank), and then generate the i = 0;.

This is to detect 0 arguments cases, and avoid the warnings with ## -- i.e: pasting TRIGGER_ZERO with something like (char*)p.

Thanks for your quick reply.

@beto

Pelle

You define TRIGGER_ZERO as a function-like macro. You later use TRIGGER_ZERO without parenthesis. Like I said, it's a problem with your code.
/Pelle