CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Dec 2008
    Posts
    91

    C++0x fails: no multiple return values

    I am kind of annoyed that C++0x doesn't support multiple return values as such:
    Code:
    int, int GetValues(int x, int y)
    {
       return (x), (y);
    }
    
    int main()
    {
       int x;
       int y;
       x,y = GetValues(1,2);
    }

    I mean, this is one of the most useful expansions that could be made to C++ and it is not added to C++0x?


    What do you guys think?


    PS: I don't need you to explain to me that I can make a function like
    Code:
    void GetValues(int x, int y, int &x, int &y)
    as this is just annoying to code (which is the problem).
    Last edited by poolisfun; May 23rd, 2010 at 03:32 PM.

  2. #2
    Join Date
    Aug 2009
    Posts
    81

    Re: C++0x fails: no multiple return values

    std::tuple, Maybe?

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: C++0x fails: no multiple return values

    Bah, a pair/tuple will do just as well, and convey a better understanding of the return value.

    And if the output is too complicated for simple pair, then io parameters were probably simpler.

    Besides, this addition would probably create so many problems. I mean C has existed for about 30 years now, and you STILL can't assign arrays (!). As long as you can't assign to arrays, or pass arrays as true pass by value, there is not a chance in hell this could work. You are basically asking for return by array with multiple types.

    Also, if I wanted to assign the result in a int [2], io parameters would be straight forward, whereas multi-return would require a temporary variable.

    Finally, I think there would be very little use to this addition, save a few special cases where, again, a pair would do just as well.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Aug 2009
    Location
    Romania->Felnac
    Posts
    48

    Re: C++0x fails: no multiple return values

    I'm pretty sure that you will not get that "feature", not even in c++2x (or whatever the next standard will be called)

    But if you point a problem where you need that, we will tell you (probably multiple ways) to achieve that the with the current (or 0x).

  5. #5
    Join Date
    Aug 2007
    Posts
    858

    Re: C++0x fails: no multiple return values

    Pretty sure C++1x is going to include tuples, which lets you do basically the same thing. You can also use Boost.Tuple now.

    Code:
    using namespace boost::tuple;
    
    tuple<int, int> GetValues(int x, int y)
    {
      return make_tuple(x, y);
    }
    
    int main()
    {
      tuple<int, int> t = GetValues(1, 2);
      
      cout << t.get<0>() << endl;
      cout << t.get<1>() << endl;
      
      return 0;
    }
    Personally I don't think I've ever encountered a situation where I needed multiple return values that weren't already encapsulated in a class or some kind of container.
    Last edited by Speedo; May 23rd, 2010 at 03:49 PM.

  6. #6
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: C++0x fails: no multiple return values

    Quote Originally Posted by Speedo View Post
    Personally I don't think I've ever encountered a situation where I needed multiple return values that weren't already encapsulated in a class or some kind of container.
    STL itself returns two values (e.g. for map::insert) by usage of std::pair.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  7. #7
    Join Date
    Dec 2008
    Posts
    91

    Re: C++0x fails: no multiple return values

    Yeah, but Boost tuple kind of makes the other method with passing pointers more convenient...

    Well, I guess if they don't want to add tuples to C++ they won't, but I don't see any problems with adding it. It will still be backwards compatible and the compiler will optimize automatically to remove temporary variables...

  8. #8
    Join Date
    Aug 2007
    Posts
    858

    Re: C++0x fails: no multiple return values

    Yeah, but Boost tuple kind of makes the other method with passing pointers more convenient...
    If you were really using it you'd (hopefully) be using typedefs to reduce the template mess.

    Well, I guess if they don't want to add tuples to C++ they won't, but I don't see any problems with adding it. It will still be backwards compatible and the compiler will optimize automatically to remove temporary variables...
    Turns out they're already in TR1

    I wouldn't be surprised if the biggest problem with directly adding the ability to return multiple values to C++ is syntax. The example you gave in the OP wouldn't work because of the comma operator. So they'd have to come up with something totally new that wouldn't interfere with anything already existing... making an already difficult to parse language that much worse.

  9. #9
    Join Date
    Jan 2009
    Posts
    1,689

    Re: C++0x fails: no multiple return values

    How about the fact that there is only one return register ($ra)? Yes, you can use the stack, but registers are thousands of times faster.

  10. #10
    Join Date
    Dec 2008
    Posts
    91

    Re: C++0x fails: no multiple return values

    Quote Originally Posted by ninja9578 View Post
    How about the fact that there is only one return register ($ra)? Yes, you can use the stack, but registers are thousands of times faster.
    The compiler just needs to convert the return values implicitly into pointers passed as parameters.

    Quote Originally Posted by Speedo View Post
    Turns out they're already in TR1

    I wouldn't be surprised if the biggest problem with directly adding the ability to return multiple values to C++ is syntax. The example you gave in the OP wouldn't work because of the comma operator. So they'd have to come up with something totally new that wouldn't interfere with anything already existing... making an already difficult to parse language that much worse.
    Sorry, I meant that they should add multiple return values directly to C++'s syntax. Too bad comma operator is overloadable...

  11. #11
    Join Date
    Oct 2008
    Posts
    1,456

    Re: C++0x fails: no multiple return values

    as other said you can already return tuples or any other "aggregate" type of your choice in C++03; furthemore, in C++0x you'll be able to use uniform initialization to pass them with a "friendly" syntax:

    Code:
    std::pair<int,int> f() { return {0,1};  }
    std::vector<int> g() { return {0,1};  }
    is this really different from "int,int f() { return (0),(1); }" ?

    IMO, it's even more descriptive and easy to write and it allows you choosing whatever "aggregate" type you like (pair,tuple, a struct, a container,...) and above all it makes clear the type of the return value ;

    for example, with the code above I know I can write "int something = f().second;" or "int something = g().back();", and so on ... I don't see any reason to add a "type-agnostic" specialized syntax for multiple return values; it really doesn't fit my C++ mindset ...

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: C++0x fails: no multiple return values

    Don't forget boost::tie, which is probably in TR1 as well:

    Code:
    std::pair<int,int> f() { return {0,1};  }
    int main()
    {
        int a, b;
        tie(a, b) = f();
    }

  13. #13
    Join Date
    Dec 2008
    Posts
    91

    Re: C++0x fails: no multiple return values

    Okay, thanks for telling me about Boost/TR1's tuples.

    I guess since the comma operator is overloadable, this the best that can be done...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured