Do not use a reference or pointer for implementation. Return a value. Do not use a reference or pointer for implementation. Return a value. The reply content is std :: tie was ignored for the worst time. After reading the previous answer, I want to mention that the language (implementation) of "true multiple return values" is not absent.
For example, Lua: Programming in Lua: 5.1
It uses the flexibility of the operand stack to put all the returned values on the operand stack for return. This is the most significant difference from the practice of "returning a tuple": tuple is an additional entity, while Lua does not have an additional entity to wrap multiple return values.
As for the original question of the subject, I agree with @ Zhao Ke's answer. You can guess it at will, but the original design determines what is going on. Only the designers at that time can know what is going on.
C ++ is the most common way to return multiple values, either by returning tuple or by using the out parameter (in other words, passing in an & parameter or an explicit pointer ). Std: tuple func1 ()
{
Return {1, 2 };
}
Void func2 ()
{
Int a, B;
Std: tie (a, B) = func1 ();
}
In this case, one is more, and multiple values are just a set. Raise objections to statements that return multiple values as just syntactic sugar.
If multiple values are returned, it may be syntactic sugar or a tool for performance improvement.
In terms of syntactic sugar, that is
ImplementationIt is equivalent to passing a struct * in C/C ++, storing the return values one by one in struct, and then the caller chooses to use it. It's just easier to support (?) at the syntax level (?) Statement
In terms of performance improvement, it is complicated and requires an introduction to assembly and some basic algorithms (operations.
To put it simply, if we pass a struct *, the compiler may not be able to optimize the read/write part of the memory. Instead, No matter we make it impossible to use all the returned values, this function should be fully written with struct, which leads to performance loss.
If the implementation uses registers or other platform features to return multiple values, the above mentioned problem does not exist.
The following long story
----------------------------
In C/C ++ and the vast majority of languages, we have two operators --/and %, Division (DIV) and modulo (remainder/MOD)
On the Intel Platform, these two operations are implemented using a single command. Here, we use the unsigned IDIV form as an example (refer to bing with manual search on the page below ):
Intel Pentium CPU Instruction Set Reference
Written as IDIV oprand
This command uses the double word consisting of R2: R0 as the divisor, the operand oprand as the divisor, and the result provider stores R0, and the remainder stores R2.
In C/C ++, if you write:
unsigned c = a/b;unsigned r = a%b;
No. Simple figure
However, this is indeed an interesting phenomenon: Most programming languages say that, following the definition of mathematical functions, a function should return only one value and learn from mathematics; while the well-known matrix lab Matlab supports multiple function return values. If a function returns only one value, the function call is an expression and can be used as a parameter to call other functions:
g(f(x))h(f(y))
Go supports returning multiple values. The function signature contains multiple types, not tuple. The implementation is similar to lua and does not require additional packaging, it is not syntactic sugar (because it does not return a struct and there is no corresponding sugar-free method ). Therefore, it has nothing to do with static types and scripting languages, but its design philosophy is different.
Go can write as follows:
package mainfunc main() {fn := func(a, b int) (int, int) {return a, b}fn(fn(1, 2))}
I think @ yf zpy is about syntactic sugar.
You can simply look at the code before and after CoffeeScript compilation.
Coffee code:
[A, B, c] = fun ()
Compiled js Code:
var a, b, c, _ref;_ref = fun(), a = _ref[0], b = _ref[1], c = _ref[2];
Return multi-value? It is just the syntactic sugar that returns the tuples and returns the values to deconstruct. C ++ can do the following:
#include
#include
#include
using namespace std;tuple
get_student(int id){ if (id == 0) return make_tuple(3.8, 'A', "Lisa Simpson"); if (id == 1) return make_tuple(2.9, 'C', "Milhouse Van Houten"); if (id == 2) return make_tuple(1.7, 'D', "Ralph Wiggum"); throw invalid_argument("id");}int main(){ double gpa1; char grade1; string name1; tie(gpa1, grade1, name1) = get_student(1); return 0;}