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).
Re: C++0x fails: no multiple return values
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.
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).
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.
Re: C++0x fails: no multiple return values
Quote:
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.
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...
Re: C++0x fails: no multiple return values
Quote:
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.
Quote:
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.
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.
Re: C++0x fails: no multiple return values
Quote:
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.
Quote:
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...
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 ...
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();
}
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...