0

Is inserting a terminating argument in send() following the VA_ARGS allowed in the specs?

#define VALIST_TERMINATOR   NULL
#define send(targ, ...)(compile_args(targ, __VA_ARGS__, VALIST_TERMINATOR))

void compile_args(int_t targ, ...)
{
    va_list a_list;
    va_start(a_list, targ);
        //parse args list
    va_end(a_list);
} 
Barmar
  • 741,623
  • 53
  • 500
  • 612
Edwin Skeevers
  • 309
  • 1
  • 8
  • 2
    Depends on what `//parse args list` is doing and how you're calling the macro. Also, `send` is the name of a library function. – dbush Dec 19 '22 at 20:41
  • Why wouldn't it be? – n. m. could be an AI Dec 19 '22 at 20:43
  • @dbush Why does the legality of the macro depend on what you do in the function? It would only affect whether this achieves the OP's intended goal (I assume they want to test the argument for `NULL` to end the loop). – Barmar Dec 19 '22 at 20:46
  • @Barmar True, I was looking at it from a "does this do what I want" view as opposed to an "is this legal" view. – dbush Dec 19 '22 at 20:50
  • OT: `int_t`? Types that end in `_t` are [reserved by POSIX](https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/functions/V2_chap02.html#tag_15_02_02) Nevermind a custom type such as `int_t` is either unnecessary, confusing, or both. – Andrew Henle Dec 19 '22 at 20:55

1 Answers1

0

I do not see anything in the standard which prevents it.

The identifier _ VA_ARGS _ shall occur only in the replacement-list of a function-like macro that uses the ellipsis notation in the parameters.

An identifier _ VA_ARGS _ that occurs in the replacement list shall be treated as if it were a parameter, and the variable arguments shall form the preprocessing tokens used to replace it.

You can very easily test it yourself:

#define VALIST_TERMINATOR   NULL
#define mymacro(targ, ...) compile_args(targ, __VA_ARGS__, VALIST_TERMINATOR)

void compile_args(unsigned *targ, ...)
{
    unsigned *x;
    va_list a_list;
    va_start(a_list, targ);
    while((x = va_arg(a_list, unsigned *)))
    {
        printf("%x\n", *x);
    }
    va_end(a_list);
} 


int main(void)
{
    mymacro(&(unsigned){1}, &(unsigned){2}, &(unsigned){3}, &(unsigned){4});
}

https://godbolt.org/z/6aPWxaoMj

0___________
  • 60,014
  • 4
  • 34
  • 74