C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Parameters with default values at arbitrary positions, with empty arguments

From: Alejandro Colomar <une+cxx_std-proposals_at_[hidden]>
Date: Sun, 29 Nov 2026 16:37:02 +0100
Hi Barry,

> Date: 2026-11-29 10:26:53-0500
> From: Barry Revzin <barry.revzin_at_[hidden]>
>
[...]
> > > On the other hand, sort(r, by: f) (or whatever syntax) actually does
> >
> > Now you need to answer more questions: which parameter is r? If you
> > name one argument, you should name them all, or you have a bunch of
> > questions that are hard to answer. We currently have issues in
> > designated initializers, precisely when one only designates some
> > member.
> >
>
> I don't think these are hard questions to answer. r is the first parameter,
> and you can provide positional arguments followed by named arguments, where
> the named arguments allow reordering (but cannot repeat positional
> arguments).

What does it mean "where the named arguments allow reordering"?

We have some experience with designated initializers, and it shows it's
not a good thing. Compilers disagree on what the following program
means:

 alx_at_devuan:~/tmp$ cat desig.c
 #include <stdio.h>

 struct {
     char str[16];
 } hello = {
     "hello world",
     .str[6] = 'W',
     "ORLD"
 };

 int main(void) {
     printf("hello.str = \"%s\"\n", hello.str);

     printf("raw bytes: ");
     for (int i = 0; i < 16; i++) {
  printf("%d ", (unsigned char)hello.str[i]);
     }
     printf("\n");

     return 0;
 }
 alx_at_devuan:~/tmp$ clang desig.c
 desig.c:7:9: warning: initializer partially overrides prior
       initialization of this subobject [-Winitializer-overrides]
     7 | .str[6] = 'W',
       | ^~~~~~~~~
 desig.c:6:5: note: previous initialization is here
     6 | "hello world",
       | ^~~~~~~~~~~~~
 1 warning generated.
 alx_at_devuan:~/tmp$ ./a.out
 hello.str = "hello WORLD"
 raw bytes: 104 101 108 108 111 32 87 79 82 76 68 0 0 0 0 0
 alx_at_devuan:~/tmp$ gcc desig.c
 alx_at_devuan:~/tmp$ ./a.out
 hello.str = "ORLD"
 raw bytes: 79 82 76 68 0 0 0 0 0 0 0 0 0 0 0 0

With function arguments, it becomes a bit simpler, since there's no
nesting, but I would rather avoid it.

Considering that per your own words it's already hard to keep track of
the absolute positions of arguments, keeping track of their relative
positions is much worse.

> There are a lot of programming languages already that allow for
> this sort of thing: Python, C#, Kotlin, Ruby, PHP, Scala, Nim, ... Some
> actually allow arbitrary interleaving between named and positional (OCaml,
> Racket) but I don't think that's necessary.

So, you don't like absolute positions, but relative positions seem fine
to you. I find that inconsistent.

> >
> > Also, this makes the names that a library uses become part of the API,
> > which is something that libraries may not want to expose.
> >
> >
> > Have a lovely night!
> > Alex
>
>
> Alternatively, it allows names to become part of the API, which is
> something that libraries would want to expose but currently cannot (or

I know some libc implementations are quite opposed to exposing those
names.


Have a lovely day!
Alex

> expose via strong typing or the designated initializer pattern).
>
> Barry

-- 
<https://www.alejandro-colomar.es>

Received on 2026-11-29 15:37:06