Date: Fri, 27 Nov 2026 14:28:23 +0100
Hi David,
> Date: 2026-11-27 13:57:10+0100
> From: David Brown <david.brown_at_[hidden]>
>
[...]
> > Hmmm, yeah, so two people here have expressed preference for 'default'.
> > When implementing this with macros in C, I also tried first to use the
> > 'default' keyword, but I couldn't find a way to do it.
>
> I would certainly greatly prefer having "default" rather than omitting the
> argument entirely
Thanks!
> - though I am still not convinced that default values in
> arbitrary positions is a good idea. (I'd rather see progress made towards
> name arguments in calls, as supported in many other languages - if that ever
> makes it to C++, then this whole idea is moot.)
I dislike named parameters. They make the call too long, and don't give
much benefit.
'default' better expresses the intent: this parameter has a default
value.
> For C, you could just have
>
> #define default
That would break switch-case and _Generic(). That's not going to work.
> and then include the "default" for clarity and portability. That would not
> force the use of "default", but it would at least allow it.
Have a lovely day!
Alex
>
> David
>
>
> >
> > So, I think 'default' seems preferable. I wish it was a bit shorter,
> > since it takes 7 characters (plus a space) for just taking a default
> > value, but I can live with it. The other major caveat is that it's not
> > backwards compatible, unlike empty args. But I'm okay with it.
> >
> > BTW, does anyone have an idea of how it could be implemented with macros
> > backwards compatibly? I think it's not possible, but maybe someone has
> > an idea. (Actually, one could stringify it, and then have a conditional
> > comparing "default" with the argument, before evaluating it, but I'm
> > looking for a zero-cost implementation.)
> >
> >
> > Have a lovely day!
> > Alex
> >
> > >
> > >
> > > Jan
> > >
> > >
> > > On Thu, 26 Nov 2026 at 12:47, Alejandro Colomar via Std-Proposals <
> > > std-proposals_at_[hidden]> wrote:
> > >
> > > > Hi!
> > > >
> > > > I implement default arguments in C with macros and a simple trick of
> > > > reversing the arguments. Here's some example:
> > > >
> > > > #include <stdio.h>
> > > >
> > > > #define DEFARG(def, arg) DEFARG_(def, arg)
> > > > #define DEFARG_(def, ...) DEFARG__(__VA_ARGS__ __VA_OPT__(,) def)
> > > > #define DEFARG__(x, ...) (x)
> > > >
> > > > #define snprintf(s, n, ...) snprintf(s, DEFARG(_Countof(s), n),
> > > > __VA_ARGS__)
> > > >
> > > > int
> > > > main(void)
> > > > {
> > > > char buf[42];
> > > >
> > > > snprintf(buf, , "%s%s", "foo", "bar");
> > > > printf("%s\n", buf);
> > > > }
> > > >
> > > > The default value is invoked by passing an empty argument, as can be
> > > > seen in the snprintf(3) call above. The mandatory empty argument makes
> > > > it safer than a completely omitted argument, as the compiler doesn't
> > > > need to go guessing what we meant. See also
> > > > <https://lore.kernel.org/lkml/abhGS0n_RsUG97Ni_at_[hidden]/> for a discussion
> > > > about a use of C++-style default arguments in the kernel, which ends up
> > > > being dangerous, and which would have been safer with mandatory empty
> > > > arguments.
> > > >
> > > > Currently, in C++, default arguments are only allowed at the end of
> > > > a function, precisely because with omitted arguments it's not easy for
> > > > the compiler to know what to do.
> > > >
> > > > I propose allowing default arguments in non-trailing parameters, and
> > > > requiring that the argument is passed as empty for invoking the default
> > > > value.
> > > >
> > > > Here's how the example from above behaves:
> > > >
> > > > alx_at_devuan:~/tmp$ gcc -Wall -Wextra -E snprintf.c | tail
> > > >
> > > > # 10 "snprintf.c"
> > > > int
> > > > main(void)
> > > > {
> > > > char buf[42];
> > > >
> > > > snprintf(buf, (_Countof(buf)), "%s%s", "foo", "bar");
> > > > printf("%s\n", buf);
> > > > }
> > > > alx_at_devuan:~/tmp$ gcc -Wall -Wextra snprintf.c
> > > > alx_at_devuan:~/tmp$ ./a.out
> > > > foobar
> > > >
> > > >
> > > > The proposal for C++ would then be so that one can write code like this:
> > > >
> > > > int foo(int a = 42, int b);
> > > >
> > > > And call it as
> > > >
> > > > foo(, 7); // Evaluates as foo(42,7)
> > > >
> > > > This would replace some cases which are currently handled with
> > > > overloads, which are a much heavier machinery (and also more difficult
> > > > to diagnose).
> > > >
> > > >
> > > > What do you think?
> > > >
> > > >
> > > > Have a lovely day!
> > > > Alex
> > > >
> > > > --
> > > > <https://www.alejandro-colomar.es>
> > > > --
> > > > Std-Proposals mailing list
> > > > Std-Proposals_at_[hidden]
> > > > https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> > > >
> >
> >
>
> Date: 2026-11-27 13:57:10+0100
> From: David Brown <david.brown_at_[hidden]>
>
[...]
> > Hmmm, yeah, so two people here have expressed preference for 'default'.
> > When implementing this with macros in C, I also tried first to use the
> > 'default' keyword, but I couldn't find a way to do it.
>
> I would certainly greatly prefer having "default" rather than omitting the
> argument entirely
Thanks!
> - though I am still not convinced that default values in
> arbitrary positions is a good idea. (I'd rather see progress made towards
> name arguments in calls, as supported in many other languages - if that ever
> makes it to C++, then this whole idea is moot.)
I dislike named parameters. They make the call too long, and don't give
much benefit.
'default' better expresses the intent: this parameter has a default
value.
> For C, you could just have
>
> #define default
That would break switch-case and _Generic(). That's not going to work.
> and then include the "default" for clarity and portability. That would not
> force the use of "default", but it would at least allow it.
Have a lovely day!
Alex
>
> David
>
>
> >
> > So, I think 'default' seems preferable. I wish it was a bit shorter,
> > since it takes 7 characters (plus a space) for just taking a default
> > value, but I can live with it. The other major caveat is that it's not
> > backwards compatible, unlike empty args. But I'm okay with it.
> >
> > BTW, does anyone have an idea of how it could be implemented with macros
> > backwards compatibly? I think it's not possible, but maybe someone has
> > an idea. (Actually, one could stringify it, and then have a conditional
> > comparing "default" with the argument, before evaluating it, but I'm
> > looking for a zero-cost implementation.)
> >
> >
> > Have a lovely day!
> > Alex
> >
> > >
> > >
> > > Jan
> > >
> > >
> > > On Thu, 26 Nov 2026 at 12:47, Alejandro Colomar via Std-Proposals <
> > > std-proposals_at_[hidden]> wrote:
> > >
> > > > Hi!
> > > >
> > > > I implement default arguments in C with macros and a simple trick of
> > > > reversing the arguments. Here's some example:
> > > >
> > > > #include <stdio.h>
> > > >
> > > > #define DEFARG(def, arg) DEFARG_(def, arg)
> > > > #define DEFARG_(def, ...) DEFARG__(__VA_ARGS__ __VA_OPT__(,) def)
> > > > #define DEFARG__(x, ...) (x)
> > > >
> > > > #define snprintf(s, n, ...) snprintf(s, DEFARG(_Countof(s), n),
> > > > __VA_ARGS__)
> > > >
> > > > int
> > > > main(void)
> > > > {
> > > > char buf[42];
> > > >
> > > > snprintf(buf, , "%s%s", "foo", "bar");
> > > > printf("%s\n", buf);
> > > > }
> > > >
> > > > The default value is invoked by passing an empty argument, as can be
> > > > seen in the snprintf(3) call above. The mandatory empty argument makes
> > > > it safer than a completely omitted argument, as the compiler doesn't
> > > > need to go guessing what we meant. See also
> > > > <https://lore.kernel.org/lkml/abhGS0n_RsUG97Ni_at_[hidden]/> for a discussion
> > > > about a use of C++-style default arguments in the kernel, which ends up
> > > > being dangerous, and which would have been safer with mandatory empty
> > > > arguments.
> > > >
> > > > Currently, in C++, default arguments are only allowed at the end of
> > > > a function, precisely because with omitted arguments it's not easy for
> > > > the compiler to know what to do.
> > > >
> > > > I propose allowing default arguments in non-trailing parameters, and
> > > > requiring that the argument is passed as empty for invoking the default
> > > > value.
> > > >
> > > > Here's how the example from above behaves:
> > > >
> > > > alx_at_devuan:~/tmp$ gcc -Wall -Wextra -E snprintf.c | tail
> > > >
> > > > # 10 "snprintf.c"
> > > > int
> > > > main(void)
> > > > {
> > > > char buf[42];
> > > >
> > > > snprintf(buf, (_Countof(buf)), "%s%s", "foo", "bar");
> > > > printf("%s\n", buf);
> > > > }
> > > > alx_at_devuan:~/tmp$ gcc -Wall -Wextra snprintf.c
> > > > alx_at_devuan:~/tmp$ ./a.out
> > > > foobar
> > > >
> > > >
> > > > The proposal for C++ would then be so that one can write code like this:
> > > >
> > > > int foo(int a = 42, int b);
> > > >
> > > > And call it as
> > > >
> > > > foo(, 7); // Evaluates as foo(42,7)
> > > >
> > > > This would replace some cases which are currently handled with
> > > > overloads, which are a much heavier machinery (and also more difficult
> > > > to diagnose).
> > > >
> > > >
> > > > What do you think?
> > > >
> > > >
> > > > Have a lovely day!
> > > > Alex
> > > >
> > > > --
> > > > <https://www.alejandro-colomar.es>
> > > > --
> > > > Std-Proposals mailing list
> > > > Std-Proposals_at_[hidden]
> > > > https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> > > >
> >
> >
>
-- <https://www.alejandro-colomar.es>
Received on 2026-11-27 13:28:31
