Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I know it's mentioned specially in the article but the C programmer in me immediately wants to use structs. They don't need to be complex or long lived or even initialized. And as a bonus there's no heap involved anywhere. Either that or just bail out of c++ and use go.


Yeah, same here, a president of a local Duff's decice fanclub, but you have to admit that proposed C++ 17 feature is pretty nice:

    ... foo()
    {
        return { 1, "bar", false };
    }

    auto { i, s, b } = foo();
It's a syntax sugar, granted, and they could push it further by adopting tuples as a native language construct with some shorthand notation (e.g. [...]), but the idea is nice.


Especially when being used to Python this would be a very welcome addition


Sweet!

Although I still cannot believe it took C++ like 30 years to come up with this.


> Either that or just bail out of c++ and use go

So either use structs or totally change language? That doesn't really make sense.


std::tuple is essentially an "anonymous" struct -- there's no allocation involved


But the members inside don't have proper names.


That's rarely a problem where MRV makes sense. Though you could always fix it by using anonymous structs whose fields are both named and positional (similar to Python's namedtuples)


I prefer to use generic structures (tuples, pairs etc.) or return proper type. One time structs are usually redeclared on multiple places. It is really hard maintain such code.


    > One time structs are usually redeclared on multiple places. It is really hard maintain such code.
Well, if you don't need forward declarations, you can define structs inline in function signature:

    #include <stdio.h>
    
    struct ret { int a; int b} hello(int c) {
        return (struct ret){ c, c + 1 };
    }
    
    int main(int argc, char **argv) {
        struct ret result = hello(2);
        printf("%d %d", result.a, result.b);
    }
I'm not sure how portable it is though, works in gcc and in whatever ideone is using: https://ideone.com/bgZUPS


G++ 5.2 complains: "new types may not be defined in a return type"

An alternative in C++14:

auto hello(int c) { struct { int a; int b; } result { c, c + 1 }; return result; }

or with gcc extensions: auto hello(int c) { return (struct { int a; int b; }) { c, c + 1 }; }


I meant something like that:

  struct x {int a, char* b}
  x foo();
  // a lot of code
  struct y {char* y, int x}
  y bar();
  //another header
  struct z {bool a, int b, char* c}
  z foo2();
It is ussually the same data structure, but someone was lazy to define it properly.


Well it's not really one time use than is it. Compound literals are your friend.




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

Search: