5 ms·
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
by jimjimjim 11y ago
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.
- wall_words 11y agostd::tuple is essentially an "anonymous" struct -- there's no allocation involved
- deleted 11y ago[deleted]
- kzhahou 11y agoBut the members inside don't have proper names.
- masklinn 11y agoThat'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)
- deleted 11y ago[deleted]
- oselhn 11y agoI 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.
- nwmcsween 11y agoWell it's not really one time use than is it. Compound literals are your friend.
- mbel 11y ago> 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 https://ideone.com/bgZUPS
- gpderetta 11y agoG++ 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 }; }
- oselhn 11y agoI 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.
- wingerlang 11y ago> 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.
- eps 11y agoYeah, 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.
- stinos 11y agoEspecially when being used to Python this would be a very welcome addition
- tdsamardzhiev 11y agoSweet! Although I still cannot believe it took C++ like 30 years to come up with this.