|
-
February 1st, 2005, 11:27 PM
#1
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?
-
February 1st, 2005, 11:41 PM
#2
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.
-
February 1st, 2005, 11:47 PM
#3
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
-
February 2nd, 2005, 05:14 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|