|
The title is slightly click-bait-y: C++20 doesn't have default parameters. It talks about how to fake it with structs. It's a pretty convincing fake, but it has one problem: the person who wrote the function needs to have written it that way. What I wish more languages had (C++, Java, etc.) is the following: Suppose I write some function that take way too many parameters: void foo(char a, short b, long c, float d, double e){} What I want, as a caller, is for the language to let me get a struct corresponding to the argument list, something like this: foo::args whatever;
whatever.e = 3.14;
// fill in the rest
And then add a bit of magic syntax so we can "unpack" that struct to actually call foo. (Extending this proposal to handle varargs is left as an exercise to the reader, because I have no idea.) |
I see two possible implementations:
- either the struct is replicated at the function call (not really efficient, but the semantics are straightforward);
- either the struct is used to prepare directly the stack for the function call (more efficient, but the semantics must be defined).
One of the issues of the second approach is that if the structure/variable is a classical one, what should happen if we use the variable after the call?
If we have to read the values inserted before the call then we have to keep a copy of the structure in the stack (so it's the same as the first approach). Or we can consider that the variable no longer exists in the scope. Which corresponds to the ownership concept in Rust for example. (but, to my knowledge, this does not exist in C++)
What is the best option? Or are there other options?
Another question, what sizeof(foo::args) should return ?