Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
[flagged] Multiple return values in C at the cost of one weird input parameter (github.com/procedural)
22 points by Procedural on April 1, 2018 | hide | past | favorite | 13 comments


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! :-)


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?


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


How about

   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


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?


Compilers hate him!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: