Liste des Groupes | Revenir à cl c |
bart wrote:Part of the reason for using the feature is for simpler, clearer code. But such solutions have costs in the form of extra syntax, extra clutter that can obscure what you're trying to do.
On 02/07/2024 20:39, Blue-Maned_Hawk wrote:Apologies—i assumed that it woudl be obvious that that's how it would be>>
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.
done.
On the other hand, some boilerplate could be alleviated: we could get a
result of
#define f(...) DEF(f, .i = 1, .j = 2, .k = "blah", __VA_ARGS__)
void DEC(f, int i, j; char * k; double l, m, n;)
{
/* actual code */
}
through the macros
#define DEF(name, ...) name##_impl((struct name##_struct){__VA_ARGS__})
#define DEC(name, ...) name##_impl(struct name##_struct {__VA_ARGS__}
name##_params)
which, while not perfect (i'm not a fan of the __VA_ARGS__ repetition
necessary in DEF), do make things better and probably a little less error-
prone.
(Apparently, the P99 preprocessor library also has some macros in it toThis is all very ingenious, but I doubt the results are worth it in practice. What short of error messages are you likely to get for a typo for example?
allow for default subroutine arguments. I have done absolutely no
research into how these work or whether they're any good.)
Les messages affichés proviennent d'usenet.