Multiple return values in C at the cost of one weird input parameter(github.com)
github.com
Multiple return values in C at the cost of one weird input parameter
https://github.com/procedural/c_multiple_return_values
13 comments
Isn't this how any multiple returns are handled? Return a tuple. Then language supports allow splatting of it, that's all.
What am I missing?
What am I missing?
You're missing the fact that it's April 1st somewhere on Earth.
I guessed the "one weird input parameter" would simply be the pointer to the structure to "return" multiple values, but it turns out I was wrong --- it is using the syntax of returning a structure by value, which depending upon the exact compiler, could be implemented in much the same way as an out-pointer parameter. i.e.
struct Foo foo = return_foo();
becomes struct Foo foo;
return_foo(&foo);
That said, the ability to pass, return, and assign structures by value seems to be one of the lesser-known features of C, so this serves as a good example.I've gotten quite used to doing just this. It started out as a habit from cpp where I relied on RVO, but for small structs the price of copying it vs copying a pointer to it seems extremely minimal apart from the temporary double allocation. Also, does anyone know if gcc or clang does something along the lines of RVO behind the scenes? I fail to see any side effects from it, so it should be entirely possible, no?
It really depends on the target architecture (and ABI), size of the struct and how fields in the struct are defined, more than if it's C or C++. For simple examples like this one: https://godbolt.org/g/ZT3bJ3 there usually will be no problem.
Yeah, just return the struct by value.
By the way, if you do C programming and haven't, read 21st Century C:
http://shop.oreilly.com/product/0636920033677.do
By the way, if you do C programming and haven't, read 21st Century C:
http://shop.oreilly.com/product/0636920033677.do
How about
And while we're taking suggestions, use a named struct, that keeps you from making up a new type for every case.
return out;
(void)zero;
instead of the whole preprocessor magic for clang?And while we're taking suggestions, use a named struct, that keeps you from making up a new type for every case.
- Step 1 return struct
- Step 2 post on HN
- Step 3 ???
- Step 4 Enjoy
- Step 2 post on HN
- Step 3 ???
- Step 4 Enjoy
Did not understand what is so special?
Same here. I thought that's pretty normal. The generic one looks pretty...normal to me. The clang version iss too much compiler voodoo and kinda makes it less readable.
how did this make the front page?
it's a demonstration of returning a struct by value...
how is this...
1. multiple return values? 2. costing one "weird input parameter"? 3. worthy of a front-page mention?
it's a demonstration of returning a struct by value...
how is this...
1. multiple return values? 2. costing one "weird input parameter"? 3. worthy of a front-page mention?
Compilers hate him!
I suppose the point is to declare a tuple directly as a return type without a typedef. Then, to return a value, use 'typeof' to declare a return variable of that anonymous type. So, the whole point is to avoid a typedef, I think:
Instead of typedef, use typeof and __auto_type. Without the bloat:
It is a bit underwhelming. But it tought me something: I did not know GCC's (and Clang's?) __auto_type extension (new since gcc 4.9, apparently). It looks great for compiler specific magic macros! :-)