Date: Fri, 27 Nov 2026 01:42:41 +0100
Hi Lénárd,
> Date: 2026-11-27 00:17:01+0000
> From: Lénárd Szolnoki via Std-Proposals <std-proposals_at_[hidden]>
>
>
>
> On 26 November 2026 11:47:25 GMT, 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?
>
> Just skipping the argument feels too subtle for me. Couldn't we do foo(default, 7) instead of foo(, 7)?
default is already a keyword in switch-case. We could reuse it, but it
feels like the mistake of reusing static for many different things.
In this case, it seems more appropriate, but I'm not sure it's a good
choice. But yeah, I considered it as an option.
The good thing about using an empty argument is that it is backwards
compatible, as it can be implemented with macros using __VA_OPT__() for
language versions that don't implement this feature.
Have a lovely night!
Alex
>
> Cheers,
> Lénárd
> Date: 2026-11-27 00:17:01+0000
> From: Lénárd Szolnoki via Std-Proposals <std-proposals_at_[hidden]>
>
>
>
> On 26 November 2026 11:47:25 GMT, 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?
>
> Just skipping the argument feels too subtle for me. Couldn't we do foo(default, 7) instead of foo(, 7)?
default is already a keyword in switch-case. We could reuse it, but it
feels like the mistake of reusing static for many different things.
In this case, it seems more appropriate, but I'm not sure it's a good
choice. But yeah, I considered it as an option.
The good thing about using an empty argument is that it is backwards
compatible, as it can be implemented with macros using __VA_OPT__() for
language versions that don't implement this feature.
Have a lovely night!
Alex
>
> Cheers,
> Lénárd
-- <https://www.alejandro-colomar.es>
Received on 2026-11-27 00:42:48
