Sujet : Re: Named arguments in C
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 02. Jul 2024, 20:50:33
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v61lm8$1p1gs$1@dont-email.me>
References : 1 2 3 4 5 6
User-Agent : Mozilla Thunderbird
On 02/07/2024 20:39, Blue-Maned_Hawk wrote:
I searched around a bit, and it seems like a more common way to implement
named arguments in C is with a pattern like this:
#define f(...) f_impl((struct f_struct){__VA_ARGS__})
void f_impl(struct f_struct { int i, j; char * k; double l, m, n; }
f_params)
{
/* actual code */
You missed out accesses to the parameters which would look like f_params.i and f_params.m.
}
int main(void)
{
f(.i = 0, .j = 2, .l = 2.5, .k = "foo", .n = 4.2, .m = 2.5);
}
This addresses a small part of it. Named parameters allow arguments to be omitted, and that requires also default values to be defined.
You can make ever more complex schemes to emulate them in C, but the boilerplate will just increase.
But at least, this allows parameters with the same type to be declared as:
double l, m, n
instead of:
double l, double m, double n