Re: [PATCH v1 1/2] kstrtox: Make _parse_integer() take variadic arguments

From: David Laight

Date: Wed Jun 03 2026 - 07:44:02 EST


On Wed, 3 Jun 2026 13:56:24 +0300
Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:

> On Wed, Jun 03, 2026 at 01:54:43PM +0300, Andy Shevchenko wrote:
> > On Wed, Jun 03, 2026 at 11:37:50AM +0100, David Laight wrote:
> > > On Tue, 2 Jun 2026 22:29:46 +0200
> > > Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:
>
> > > > Instead of having different functions that just use default parameters,
> > > > combine those to use variadic arguments, so the user may call it using
> > > > the same name.
> > >
> > > Adding a default final parameter can be done generically:
> >
> > Only one?
> >
> > > #define one(v) v
> > > #define first(v, ...) v
> > > #define dflt(d, ...) first(__VA_OPT__(one(__VA_ARGS__) ,) d)
> > >
> > > int foo(int, int);
> > > #define foo(a, ...) foo(a, dflt(42, ## __VA_ARGS__))
> > >
> > > See: https://godbolt.org/z/x5aao7reK
> >
> > I know, we support some GCC versions that do not provide it.
> >
> > 551d44200152 ("default_gfp(): avoid using the "newfangled" __VA_OPT__ trick")
>
> I stand corrected, it's all about sparse. Since this is the generic header,
> I would also avoid using VA_OPT even if it allows more than one optional
> argument.

I'm not sure multiple optional arguments are overly useful.
Starts getting difficult knowing what they are for.

Really the C standard should have added named parameters ages ago.
Perhaps as:
int f(int a, int b, int c = 0);
then:
x = f(b:17, a:42);
then you could have default values for any parameters.
Would also be less error prone for functions with lots of flag parameters.

For sparse it only has to expand to valid C.
So how about something like the following for sparse builds?

#define dlft(d, ...) (one(__VA_ARGS__) + 0 ?: (d))

-- David