by jimjimjim · 10y ago · 0 comments
... 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.Although I still cannot believe it took C++ like 30 years to come up with this.
So either use structs or totally change language? That doesn't really make sense.
> 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/bgZUPSAn 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 }; }
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.