CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2003
    Location
    Karachi, Pakistan
    Posts
    38

    Multiple output arguments

    The C++ alows returning more than one value from a function through pointers and references.
    It would have been nice if we could declare multiple output arguments on the left,
    for example : -

    int, int GetXY()
    {
    int x, y;
    --------------;
    return x, y;
    }

    and call this as

    int x, y;
    ----------;
    x, y = GetXY();
    or
    (x, y) = GetXY();

    Note how much this syntax looks natural to the C++ language.

    What do you think?

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Multiple output arguments

    So far, the STL only provides std::pair. But if you need more than 2, you may want to consider using boost::tuple which can be downloaded from http://www.boost.org.

  3. #3
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: Multiple output arguments

    Unless you are contemplating writing your own compiler, I am fairy certain there is no way you can make that work. (You might be able to hack something similar up by overloading the , operator though.)

    Furthermore I don't quite see the point. What you are proposing can easily be acieved by returning a structure with the values you need to return. (std:air can be used for such a purpose)
    Code:
    std::pair<int, int> GetXY()
        {
        int x, y;
        return std::make_pair(x, y);
        }
    EDIT: Too slow.
    Insert entertaining phrase here

  4. #4
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: Multiple output arguments

    If two values are sufficiently closely related to warrant being returned at the same time from a function, then those two values deserve to be encapsulated into a single object. From the example, I would assume that x and y are coordinates of a 2d point. You should be passing around Point2D objects in that case, not separate values.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


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