|
-
May 23rd, 2010, 03:28 PM
#1
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.
-
May 23rd, 2010, 03:38 PM
#2
Re: C++0x fails: no multiple return values
-
May 23rd, 2010, 03:40 PM
#3
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.
-
May 23rd, 2010, 03:42 PM
#4
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).
-
May 23rd, 2010, 03:47 PM
#5
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.
-
May 23rd, 2010, 05:04 PM
#6
Re: C++0x fails: no multiple return values
 Originally Posted by Speedo
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.
-
May 23rd, 2010, 05:50 PM
#7
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...
-
May 23rd, 2010, 06:14 PM
#8
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.
-
May 23rd, 2010, 06:23 PM
#9
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.
-
May 23rd, 2010, 06:53 PM
#10
Re: C++0x fails: no multiple return values
 Originally Posted by ninja9578
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.
 Originally Posted by Speedo
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...
-
May 24th, 2010, 10:00 AM
#11
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 ...
-
May 24th, 2010, 11:23 AM
#12
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();
}
-
May 24th, 2010, 09:16 PM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|