Sorry to be blunt -- I mean no offense, we all started somewhere. To me, it reads like a beginners experiment. All that #pragma stuff, that first param ('zero') that is all not needed for the effect. The 'zero' and 'return;' stuff reads to me like someone is assuming typeof() evaluates its argument so someone must do somethings against it executing. It looks bloated with stuff a beginner does not realise is unnecessary.<p>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:<p><pre><code> typedef struct { int x; int y; } foo_result_t;
foo_result_t foo(int a, int b) {
return (foo_result_t){ a*a, a/b };
}
int main(void) {
foo_result_t a = foo(40,2);
}
</code></pre>
Instead of typedef, use typeof and __auto_type. Without the bloat:<p><pre><code> struct { int x; int y; } foo(int a, int b)
{
typeof(foo(a,b)) out = { a*a, a/b };
return out;
}
int main(void) {
__auto_type a = foo(40,2);
}
</code></pre>
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! :-)
Isn't this how any multiple returns are handled? Return a tuple. Then language supports allow splatting of it, that's all.<p>What am I missing?
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.<p><pre><code> struct Foo foo = return_foo();</code></pre>
becomes<p><pre><code> struct Foo foo;
return_foo(&foo);
</code></pre>
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.
Yeah, just return the struct by value.<p>By the way, if you do C programming and haven't, read 21st Century C:<p><a href="http://shop.oreilly.com/product/0636920033677.do" rel="nofollow">http://shop.oreilly.com/product/0636920033677.do</a>
How about<p><pre><code> return out;
(void)zero;
</code></pre>
instead of the whole preprocessor magic for clang?<p>And while we're taking suggestions, use a named struct, that keeps you from making up a new type for every case.
how did this make the front page?<p>it's a demonstration of returning a struct by value...<p>how is this...<p>1. multiple return values?
2. costing one "weird input parameter"?
3. worthy of a front-page mention?