Skip to content

Commit 9c17262

Browse files
committed
Clarify passing wrapped primitives by value
1 parent c68a09a commit 9c17262

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

cppguide.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,6 +2619,28 @@ <h3 id="Pass_By_Value">Pass by Value</h3>
26192619
<li><a href="https://stackoverflow.com/questions/10231349/are-the-days-of-passing-const-stdstring-as-a-parameter-over">Stack Overflow: Are the days of passing const std::string& as a parameter over?</a></li>
26202620
</ul>
26212621
</p>
2622+
2623+
<h4 id="Pass_By_Value_Primitives">Pass by Value: Primitives</h4>
2624+
2625+
<p>Prefer to pass primitives (e.g., <code>int</code> or <code>double</code>)
2626+
by value, not by const reference.</p>
2627+
2628+
<p>The same goes for copyable types that are tantamount to primitives,
2629+
such as type-safe integers (e.g., <code>drake::TypeSafeIndex</code> or
2630+
<code>drake::Identifier</code>) and thin wrappers
2631+
(e.g., <code>std::optional&lt;double&gt;</code>, because it's akin to a
2632+
pair of <code>(bool, double)</code>).
2633+
2634+
<p>Drake's
2635+
<a href="/doxygen_cxx/group__default__scalars.html">default scalars</a>
2636+
(known conventionally as <code>&lt;typename T&gt;</code>) are often thought
2637+
of as a <code>double</code> because they denote a mathematical scalar,
2638+
but because scalar types other than <code>double</code> are not primitives,
2639+
templated scalars should be passed as <code>const T&</code>, not by value.</p>
2640+
2641+
<p>See also <a href="#Use_of_const">Use of const</a> for an explanation of
2642+
whether arguments passed by-value may be declared <code>const</code>.</p>
2643+
26222644
</div>
26232645

26242646
<h3 id="Friends">Friends</h3>

0 commit comments

Comments
 (0)