Re:Named parameters in C
by nitrogen on Monday July 14, 2014 @06:12AM
(#8030258)
Named arguments can be done by passing a struct as the only parameter to a function, without the complicated macros. Missing arguments become 0, so you get optional parameters for free.
[ Parent
| on HN ]
That is really cool.
Combine this with struct definitions in function bodies and I see a great teamup with variadic functions.
Take a theoretical struct_printf, which prints out non promoted data with struct packing (not variadic stack packing, which requires type promotion)
/* Only uses va_start and va_arg to*/
void struct_printf(const char* format, ...){
...
}
void print_foo(){
struct foo_t{
float f;
uint16_t u2;
uint8_t x;
} input;
struct_printf("No type promotion here: %f %hu %hhx\n",
(struct foo_t){ .f = 0.1, .u2 = 1000, .x = 0xF2});
}
Now we can actually pass what we mean to pass without the compiler changing it.NOTE: After I wrote this I'm not actually sure what va_args does with a struct or how to get a pointer to the first member...
NOTE: After I wrote this I'm not actually sure what va_args does with a struct or how to get a pointer to the first member...
I think you would still need to know the exact type inside of struct_printf(), unless you knew exactly how the compiler would pack a struct onto the stack or registers, and that it was exactly the same as individual parameters.
Note, %hu and %hhx are not portable ways to print uint16_t and uint8_t.
Instead, you're supposed to #include <inttypes.h> and use:
"No type promotion here: %f %"PRIu16" %"PRIx8"\n"
Re:Named parameters in C
by CountHackulus on Monday July 14, 2014 @10:00AM
(#8030924)
And that's almost exactly what the macro expands to. It's just some nice syntactic sugar on top.
The function declaration with the macro is a bit awkward, IMO. I don't mean to downplay the awesomeness of the macro approach, though; FOR_EACH and putting __VA_ARGS__ at the beginning of the macro argument list isn't something I had considered before.
Re:Named parameters in C
by eliteraspberrie on Monday July 14, 2014 @09:48AM
(#8030851)
This (compound literals) also has the advantage of being strictly valid C, since compound statements are a GNU extension.
I find your statement puzzling. A GNU extension is by definition not “strictly valid C”, compound literals are valid C because they are defined in C99.
Compound literals are valid C; compound statements, used in the macros in the article, are a GNU extension.
Oh right, I misread the previous comment, thanks.
What's the point in that? Then you have to write all your functions to accept structs instead of natural arguments.
http://c2.com/cgi/wiki?ParameterObject
Sometimes it's a good way to go.
Re:Named parameters in C
by simgidacav on Monday July 14, 2014 @12:03PM
(#8031535)
...agreed, what's the point? Besides, a lot of includes are already a mess of #defines, do we really need another bunch of them?
Wow, that's really interesting. I had no idea this was possible. Do you know of any links to tutorials or blog posts on this? I'd love to see a more detailed example.
A value of 0 is not the same thing as a missing value.
That's true, but the parent comment never claimed that, only that missing arguments are assigned the value 0 in the resultant struct.
“[…] so you get optional parameters for free”