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.
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:
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);
}
Instead of typedef, use typeof and __auto_type. Without the bloat:
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);
}
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! :-)
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.
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.
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! :-)