Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a matter of style. At Google (see <a href="http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml" rel="nofollow noreferrer">Google C++ style guidelines</a>), the following would be preferred:</p> <pre><code>bool CalculateSomeValue( const vector&lt;string&gt;&amp; input, map&lt;string, double&gt;* result); </code></pre> <p>This is because using a pointer requires the explicit use of an ampersand at the call site:</p> <pre><code> CalculateSomeValue(input, &amp;result); </code></pre> <p>As opposed to the way it would be invoked with a reference type:</p> <pre><code> CalculateSomeValue(input, result); </code></pre> <p>By forcing the use of an ampersand in cases where parameters are modified, it is clear at the call site what will happen. When using references, it becomes necessary to lookup the documentation for each and every function to know whether it has the potential to modify its input parameters.</p> <p>However, the use of a pointer has its downsides, too. In particular, the use of a pointer means that you shift responsibility of handling null from the caller where the pointer would be dereferenced (if the variable is a pointer and not merely an address-of expression with a local variable) to the function. That is, when using a pointer type, <code>CalculateSomeValue</code> needs to check for <code>nullptr</code> (or needs to clearly document that it requires this checking in the caller), whereas the use of a reference type is self-documenting and clearly indicates that a non-null reference is expected.</p> <p>For this particular situtation, I personally highly recommend a hybrid approach:</p> <pre><code>bool CalculateSomeValue( const std::vector&lt;std::string&gt;&amp; input, Output&lt;map&lt;string, double&gt;&gt; result); </code></pre> <p>... where <code>Output&lt;T&gt;</code> is created by a function with the signature:</p> <pre><code> template&lt;typename T&gt; Output&lt;T&gt; WriteTo(T&amp; output_ref); </code></pre> <p>... and where <code>Output&lt;T&gt;</code> overloads operators <code>-&gt;</code>, <code>*</code>, etc. This basically forces the call sites to be explicit in the fact that the input will be mutated by requiring:</p> <pre><code> CalculateSomeValue(input, WriteTo(result)); </code></pre> <p>... as opposed to:</p> <pre><code> CalculateSomeValue(input, result); </code></pre> <p>... while simultaneously gaining the non-null semantics/syntax of references.</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