Re:Returning multiple values from functions in C++
by jimjimjim on Sunday March 06, 2016 @09:02PM
(#11236633)
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.
[ Parent
| on HN ]
Re:Returning multiple values from functions in C++
by eps on Monday March 07, 2016 @05:19AM
(#11238072)
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.
Re:Returning multiple values from functions in C++
by stinos on Monday March 07, 2016 @06:40AM
(#11238340)
Especially when being used to Python this would be a very welcome addition
Re:Returning multiple values from functions in C++
by tdsamardzhiev on Monday March 07, 2016 @07:17AM
(#11238451)
Sweet!
Although I still cannot believe it took C++ like 30 years to come up with this.
Re:Returning multiple values from functions in C++
by wingerlang on Monday March 07, 2016 @03:34AM
(#11237781)
> 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.
Re:Returning multiple values from functions in C++
by wall_words on Sunday March 06, 2016 @10:27PM
(#11236925)
std::tuple is essentially an "anonymous" struct -- there's no allocation involved
Re:Returning multiple values from functions in C++
by kzhahou on Monday March 07, 2016 @12:30AM
(#11237302)
But the members inside don't have proper names.
Re:Returning multiple values from functions in C++
by masklinn on Monday March 07, 2016 @09:23AM
(#11238983)
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)
Re:Returning multiple values from functions in C++
by oselhn on Monday March 07, 2016 @02:16AM
(#11237568)
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.
Re:Returning multiple values from functions in C++
by mbel on Monday March 07, 2016 @04:25AM
(#11237910)
> 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
Re:Returning multiple values from functions in C++
by gpderetta on Monday March 07, 2016 @05:09AM
(#11238037)
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 }; }
Re:Returning multiple values from functions in C++
by oselhn on Monday March 07, 2016 @07:18AM
(#11238456)
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.