lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Am 29.01.2015 um 11:33 schröbte Philippe Lhoste:
On 26/01/2015 10:00, Philipp Janda wrote:
Am 26.01.2015 um 08:51 schröbte William Ahern:
The macro arguments might themselves be comma expressions. So the
parentheses aren't useless.
You can't have comma expressions without parentheses in argument lists.

     #define X(a, b) ...

     X(1, 2); /* ok */
     X(1, 2, 3); /* constraint violation: wrong number of arguments */
     X((1, 2), 3); /* ok, using comma expression as first argument */
What about vararg functions? (My C is rusty, so it can be an invalid
remark!)
The argument lists of vararg functions (or functions without prototype) 
are the same. You won't get a compiler diagnostic, but you still have 
the wrong number of arguments. But we are talking about function-like 
macros anyway. Basically, if it parses as one argument in the argument 
list of a function-like macro, it will parse as one argument in the 
argument list of a function call in the macro replacement without 
additional parentheses. However, there is one corner case (and it in 
fact has to do with a combination of comma expressions and vararg 
functions):
You *can* have comma expressions without parentheses in the argument 
list of functions (but not function-like macros) if the comma expression 
is in the middle part of a ternary expression. So
    f(a<b ? a,b : b);

will call `f` with one argument if `f` is a function, but two arguments if `f` is a function-like macro. As a consequence the following
    #define X(a, b) printf("%d %d\n", a, b)
    X(1<2 ? 1,2 : 3);

will lead to a `printf` call with one missing argument. On the other hand

    #define X(a, b) printf("%d %d\n", (a), (b))
    X(1<2 ? 1,2 : 3);

will be flagged as a syntax error by the compiler. So in some rare cases the extra parentheses can help you catch incorrect usage of a macro.
Of course, this discussion is mostly theoretical, I would say as above
the parentheses are here mostly for consistency (with other macros) and
common practice.
As the OP noted, the Lua source isn't consistent in this regard.


Philipp