Problem:Having a single return value means we have to pass the other return values as parameters as a reference or a pointer. This
He has a valid point, I think. Returning tuples and especially unpacking on the other end isn't as easy as it could be in some languages with core language support for this- for example, Lua.
Problem:Having a single return value means we have to pass the other return values as parameters as a reference or a pointer. This makes the programs difficult to understand. Moreover we can solve most of the problems mentioned in http://www.openstd.org/JTC1/SC22/WG21/docs/papers/2013/n3538.html.
Solution:(int; float) foo (int);
Probably a contextual keyword returns can be added to specify that the first pair of parentheses represents return values. So we will be passing only in parameters and in out parameters to the function. All the out parameters are only returned.With this we can write the codeint a;float b;(a; b) = foo(a, b);
(int c; float d) = foo(a, b);
We can pass the return values to functions. We can write foo( foo(a, b) );
We can compare the return values. The semantics can be defined as the operation should be true for all the return values.foo(a, b) == foo(c, d), foo(a, b) >= foo(c, d) etc.,
We cab do the following also(c, d) == foo(a, b), (c, d) > foo(a, b) etc.,
(a, b) = (b, a) swaps the values. It should generate the code equivalent to
There are some problem areas like type casting. Probably they can be prohibited. Other issues may remain and need to be discussed.
I agree. I'm not saying that there is a core feature that will work for C++, since language-level support for tuples covers a whole lot more than his simple use case (although it could, potentially, be used to fix the very broken uniform initialization). What I am saying is that it's not a problem which is entirely solved by std::tuple, and it would be a desirable feature to have.
--
---
You received this message because you are subscribed to a topic in the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this topic, visit https://groups.google.com/a/isocpp.org/d/topic/std-proposals/VROimyg83fc/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en.
--
An expression starting with '{' is certainly problematic. The problem here isn't so much how to parse it (that actually turns out to be tractable with basically no overhead and no extra lookahead) but more that it seriously harms diagnostics and error recovery in some important cases. There are some genuinely ambiguous cases, too, if we allow the general form:{} + x; // empty compound-statement followed by unary operator, or binary operator with empty braced-init-list on LHS?
Using parens, as the original proposal suggests, may avoid this particular problem, but I strongly suspect there will still be ambiguities.
I've been experimenting with a different approach to this problem: allow a restricted form of simple-declaration as an lvalue expression, then:std::tie(int a, int b, const char *c) = f();This also lessens the need to use tuples at all, since you can write out-parameters like this:f(int a, int b, const char *c);
This still has some harmful impact on error recovery (the above could be a declaration of 'f' missing a return type, for instance), and it requires restricting the declarator forms to avoid declaration/expression ambiguity for cases like 'f(int(a))'.Both this and the "{ a, b, c } = " form have an additional (potentially significant) problem: they require a, b, and c to be default-constructible and move-assignable.
Given the alternatives in the design space, returning a tuple and capturing it with 'auto' seems reasonable to me, aside from the syntactic inconvenience of accessing a tuple element. If the caller needs names for the fields, maybe returning a tuple with unnamed fields was a poor design choice, and returning a struct would be a better option?
--