Live data from Hacker News

Returning multiple values from functions in C++

eli.thegreenplace.net

21–30 of 67 posts

Re: Returning multiple values from functions in C++

#21
post #18

Earlier quoted context omitted.

I can't be the only one who finds "INT_VALUE" both superfluous and unnecessarily ugly?

You're right, I have been thinking about this naming as well. I think in this context, "INT_PARAM" would be better. I like to typedef it because this way it's easier to replace with a hardware specific integer type later on if I decide to do so. Anyways, the "VALUE" naming is coming from Fortran, which is by default pass-by-reference, so you use it to get pass-by-value. Edit: Thinking about it some more, "INT_VALUE"…

> Doing this reminds me that I'm (probably) imposing more register pressure by passing by value.

Not for "int"s, I would be very surprised if sizeof(int*) < sizeof(int) on your system.

Re: Returning multiple values from functions in C++

#22
post #19

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.

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.

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

Re: Returning multiple values from functions in C++

#23

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.

> 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++

#24
post #21

Earlier quoted context omitted.

You're right, I have been thinking about this naming as well. I think in this context, "INT_PARAM" would be better. I like to typedef it because this way it's easier to replace with a hardware specific integer type later on if I decide to do so. Anyways, the "VALUE" naming is coming from Fortran, which is by default pass-by-reference, so you use it to get pass-by-value. Edit: Thinking about it some more, "INT_VALUE"…

> Doing this reminds me that I'm (probably) imposing more register pressure by passing by value. Not for "int"s, I would be very surprised if sizeof(int*) < sizeof(int) on your system.

Good point, that was a bit of a brainfart of mine.

Re: Returning multiple values from functions in C++

#25
post #19

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.

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 
    
    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++

#26
post #25
post #19

Earlier quoted context omitted.

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 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…

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++

#27

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.

Re: Returning multiple values from functions in C++

#28
post #15

Ada has in, out and in/out parameters which adds a little more semantic to C and C++'s pointer parameters. Author mentions Common Lisp's multiple values, and one of the main feature of multiple values is that they are optional: the caller does not need to use secondary values. Even though floor returns 2 values, the following is a valid expression: (+ (floor x) 2) Only the primary value is used. A compiler typically…

C# has out if I remember correctly. Haven't used it in months (C# that is) but it pretty much does the job as far as I've used it.

Re: Returning multiple values from functions in C++

#29
The problem with deconstructing a tuple is that you can easily make mistake about the order of its members. The struct is a much better solution to me, and you can go over the problem of the weirdly named one-off struct by using the iod library (https://github.com/matt-42/iod):

  foo() { return D(_id = "test", _age = "42" }; }

  auto r = foo();
  std::cout 

Re: Returning multiple values from functions in C++

#30
post #27

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
Post reply on HN