Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h3>tuples</h3> <p>I think i agree with you that the issue with what position corresponds to what variable can introduce confusion. But i think there are two sides. One is the <em>call-side</em> and the other is the <em>callee-side</em>:</p> <pre><code>int remainder; int quotient; tie(quotient, remainder) = div(10, 3); </code></pre> <p>I think it's crystal clear what we got, but it can become confusing if you have to return more values at once. Once the caller's programmer has looked up the documentation of <code>div</code>, he will know what position is what, and can write effective code. As a rule of thumb, i would say not to return more than 4 values at once. For anything beyond, prefer a struct. </p> <h3>output parameters</h3> <p>Output parameters can be used too, of course:</p> <pre><code>int remainder; int quotient; div(10, 3, &amp;quotient, &amp;remainder); </code></pre> <p>Now i think that illustrates how tuples are better than output parameters. We have mixed the input of <code>div</code> with its output, while not gaining any advantage. Worse, we leave the reader of that code in doubt on what could be the <em>actual</em> return value of <code>div</code> be. There <em>are</em> wonderful examples when output parameters are useful. In my opinion, you should use them only when you've got no other way, because the return value is already taken and can't be changed to either a tuple or struct. <code>operator&gt;&gt;</code> is a good example on where you use output parameters, because the return value is already reserved for the stream, so you can chain <code>operator&gt;&gt;</code> calls. If you've not to do with operators, and the context is not crystal clear, i recommend you to use pointers, to signal at the call side that the object is actually used as an output parameter, in addition to comments where appropriate.</p> <h3>returning a struct</h3> <p>The third option is to use a struct:</p> <pre><code>div_result d = div(10, 3); </code></pre> <p>I think that definitely wins the award for <strong>clearness</strong>. But note you have still to access the result within that struct, and the result is not "laid bare" on the table, as it was the case for the output parameters and the tuple used with <code>tie</code>.</p> <p>I think a major point these days is to make everything as generic as possible. So, say you have got a function that can print out tuples. You can just do </p> <pre><code>cout &lt;&lt; div(10, 3); </code></pre> <p>And have your result displayed. I think that tuples, on the other side, clearly win for their <strong>versatile</strong> nature. Doing that with div_result, you need to overload operator&lt;&lt;, or need to output each member separately. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload