tuples
I think I agree with you that the problem with which position corresponds to the variable that may introduce confusion. But I think there are two sides. One of them is the call-side, and the other is the call side:
int remainder; int quotient; tie(quotient, remainder) = div(10, 3);
I think itโs clear what we got, but it can become confusing if you need to return more values โโat once. As soon as the callerโs programmer has looked through the div documentation, he will know which position is and can write the effective code. As a rule, I would say not to return more than 4 values โโat once. For anything, prefer structure.
output parameters
Of course, you can use the output parameters:
int remainder; int quotient; div(10, 3, "ient, &remainder);
Now I think this illustrates how tuples are better than output parameters. We mixed the input of the div with its output, but did not get any benefits. Even worse, we leave the reader of this code with a doubt about what the actual return value of the div be might be. There are great examples when output options are useful. In my opinion, you should use them only when you have no other way, because the return value has already been completed and cannot be changed to either a tuple or a structure. operator>> is a good example of where you use the output parameters, because the return value is already reserved for the stream, so you can bind operator>> calls. If you are not associated with operators and the context is not crystal clear, I recommend that you use pointers to signal on the call side that the object is actually used as an output parameter, in addition to comments where necessary.
returns struct
The third option is to use struct:
div_result d = div(10, 3);
I think that definitely wins the reward for clarity . But note that you still need to access the result inside this structure, and the result is not "exposed" in the table, as was the case with the output parameters and the tuple used with tie .
I think that today it is important to make everything as general as possible. So let's say you have a function that can print tuples. You can just do
cout << div(10, 3);
And display your result. I think that tuples, on the other hand, clearly win for their universal nature. When doing this with div_result, you need to overload operator </ or display each element separately.