Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.std.c |
| Subject | Re: Why doesn't __VA_LIST__ just eat the prior comma? |
| Date | 2022-04-18 20:43 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87bkwygotu.fsf@bsb.me.uk> (permalink) |
| References | <20220413165157.844@kylheku.com> <87czhju7ml.fsf@bsb.me.uk> <20220418093916.295@kylheku.com> |
Kaz Kylheku <480-992-1380@kylheku.com> writes:
> On 2022-04-14, Ben <ben.usenet@bsb.me.uk> wrote:
>> Kaz Kylheku <480-992-1380@kylheku.com> writes:
>>
>>> There is a well-known problem that a macro like
>>>
>>> #define foo(x, ...) something(whatever, __VA_ARGS__)
>>>
>>> cannot work when the trailing argument list is empty because
>>> you get the expansion something(whatever, )
>>>
>>> GNU C provides ##__VA_ARGS__ which behaves like __VA_ARGS__ in
>>> the nonempty case, and eats the prior comma in the empty case.
>>>
>>> C++20 provides __VA_OPT__ which is used like __VA_OPT__(,) to
>>> conditionally eat the comma. (GNU C has this also).
>>>
>>> The question is: why wouldn't you just fix the semantics of __VA_ARG__
>>> so that this is not necessary?
>>>
>>> When would you ever want to interpolate __VA_ARGS__ into a replacement
>>> sequence such that if it is empty, and placed after a comma, that
>>> comma does not disappear?
>>
>> You might not want it (as a design feature), but there is probably code
>> out there that now depends on it.
>
> That's my question: what is the form of this dependency? What is an
> example of something that breaks if __VA_ARGS__ eats the prior comma
> when it has an empty expansion?
I don't know of an example.
> Of course, we can write regression test cases which verify that
> the comma is not eaten, and I can think of ways of doing that:
>
> #define mac(...) foo(, __VA_ARGS__)
> #define xstr(x) str(x)
> #define str(x) #x
>
> assert (strcmp(xstr(mac()), "foo(, )") == 0);
>
> printf("%s\n", xstr(mac()));
>
> I mean, some way of relying on the comma that is actually useful.
The code does not have to find it actually useful for it to break on a
change. If someone, somewhere, ended up having to work around this
issue, that code will break even if the programmer might now be glad of
the opportunity to simplify it.
--
Ben.
Back to comp.std.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Why doesn't __VA_LIST__ just eat the prior comma? Kaz Kylheku <480-992-1380@kylheku.com> - 2022-04-14 00:01 +0000
Re: Why doesn't __VA_LIST__ just eat the prior comma? Ben <ben.usenet@bsb.me.uk> - 2022-04-14 14:17 +0100
Re: Why doesn't __VA_LIST__ just eat the prior comma? Kaz Kylheku <480-992-1380@kylheku.com> - 2022-04-18 16:59 +0000
Re: Why doesn't __VA_LIST__ just eat the prior comma? Ben <ben.usenet@bsb.me.uk> - 2022-04-18 20:43 +0100
Re: Why doesn't __VA_LIST__ just eat the prior comma? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-04-19 02:04 -0700
Re: Why doesn't __VA_LIST__ just eat the prior comma? Kaz Kylheku <480-992-1380@kylheku.com> - 2022-04-20 02:02 +0000
Re: Why doesn't __VA_LIST__ just eat the prior comma? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2022-05-14 10:35 -0700
csiph-web