Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 49 additions & 22 deletions cppguide.html
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ <h3 id="Namespaces">Namespaces</h3>

namespace internal { // Internal, not part of the API.
namespace sidetable = ::pipeline_diagnostics::sidetable;
} // namespace internal
}

inline void my_inline_function() {
// Local to a function.
Expand Down Expand Up @@ -2292,7 +2292,7 @@ <h3 id="Rvalue_references">Rvalue References</h3>

<p class="cons"></p>
<ul>
<li>Rvalue references are not yet widely understood. Rules like reference
<li>Rvalue references are not widely understood. Rules like reference
collapsing and the special deduction rule for forwarding references
are somewhat obscure.</li>

Expand Down Expand Up @@ -2621,11 +2621,13 @@ <h3 id="Run-Time_Type_Information__RTTI_">Run-Time Type
instance of a base class is in fact an instance of a
particular derived class, then a
<code>dynamic_cast</code> may be used freely on the
object. Usually one
can use a <code>static_cast</code> as an alternative in
such situations.</p>
object. Usually

<a href="https://github.com/abseil/abseil-cpp/blob/master/absl/base/casts.h">
<code>absl::down_cast</code></a> provides an even better
alternative.

<p>Decision trees based on type are a strong indication
</p><p>Decision trees based on type are a strong indication
that your code is on the wrong track.</p>

<pre class="badcode">if (typeid(*data) == typeid(D1)) {
Expand Down Expand Up @@ -2676,7 +2678,12 @@ <h3 id="Casting">Casting</h3>
<p class="decision"></p>
<p>In general, do not use C-style casts. Instead, use these C++-style
casts when explicit type conversion is necessary.
</p>
Some are part of the standard C++ language,
and some are defined in

<a href="https://github.com/abseil/abseil-cpp/blob/master/absl/base/casts.h">
<code>absl/base/casts.h</code></a>. Use the first that
<b>correctly applies</b>:</p>

<ul>
<li>Use brace initialization to convert arithmetic types
Expand Down Expand Up @@ -2704,7 +2711,10 @@ <h3 id="Casting">Casting</h3>
subclass. In this last case, you must be sure your object is
actually an instance of the subclass.</li>


<li>Use <code>absl::down_cast</code>
to cast a pointer from a superclass
to a subclass. You must be sure your object is actually
an instance of the subclass.</li>

<li>Use <code>const_cast</code> to remove the
<code>const</code> qualifier (see <a href="#Use_of_const">const</a>).</li>
Expand Down Expand Up @@ -3282,6 +3292,10 @@ <h3 id="Type_deduction">Type Deduction (including auto)</h3>
void f(T t);

f(0); // Invokes f&lt;int&gt;(0)</pre>
A lambda expression can also have template parameters, which are deduced in the same way:
<pre class="neutralcode">// Sort `vec` in decreasing order
std::sort(vec.begin(), vec.end(), []&lt;typename T&gt;(T lhs, T rhs) { return lhs &gt; rhs; });
</pre>
</dd>
<dt><a href="https://en.cppreference.com/w/cpp/language/auto"><code>auto</code> variable declarations</a></dt>
<dd>A variable declaration can use the <code>auto</code> keyword in place
Expand Down Expand Up @@ -3316,11 +3330,10 @@ <h3 id="Type_deduction">Type Deduction (including auto)</h3>
rely on type deduction; it's just an alternative syntax for an explicit
return type.
</dd>
<dt><a href="https://isocpp.org/wiki/faq/cpp14-language#generic-lambdas">Generic lambdas</a></dt>
<dd>A lambda expression can use the <code>auto</code> keyword in place of
one or more of its parameter types. This causes the lambda's call operator
to be a function template instead of an ordinary function, with a separate
template parameter for each <code>auto</code> function parameter:
<dt><a href="https://en.cppreference.com/w/cpp/language/auto.html"><code>auto</code> parameters</a></dt>
<dd>A function or lambda expression can use the <code>auto</code> keyword in place of
one or more of its parameter types. This causes it to be a function template instead of an
ordinary function, with a separate template parameter for each <code>auto</code> function parameter:
<pre class="neutralcode">// Sort `vec` in decreasing order
std::sort(vec.begin(), vec.end(), [](auto lhs, auto rhs) { return lhs &gt; rhs; });</pre>
</dd>
Expand Down Expand Up @@ -3462,16 +3475,30 @@ <h4>Return type deduction</h4>
<em>is</em> the interface. In particular, public functions in header files
should almost never have deduced return types.</p>

<h4>Parameter type deduction</h4>
<h4>Function parameter type deduction</h4>

<p>Avoid using parameter type deduction (using either <code>auto</code> or template
parameters) if you can give the parameter a specific type instead. If it will only
be called with one type, it will almost always be clearer to make that type explicit,
unless it is called very close to where it's defined (so that the reader can easily
see both), or it's a lambda being passed to an interface so well-known that it's
obvious what type it will eventually be called with (e.g., the <code>std::sort</code>
example above)</p>

<p><code>auto</code> parameter types for lambdas should be used with caution,
because the actual type is determined by the code that calls the lambda,
rather than by the definition of the lambda. Consequently, an explicit
type will almost always be clearer unless the lambda is explicitly called
very close to where it's defined (so that the reader can easily see both),
or the lambda is passed to an interface so well-known that it's
obvious what arguments it will eventually be called with (e.g.,
the <code>std::sort</code> example above).</p>
<p>Do not use <code>auto</code> parameters in non-lambda functions. Use named template parameters
instead, because they are more explicit about the fact that the function is a template:</p>
<pre class="badcode">void F(auto arg) { ... }
</pre>
<pre class="goodcode">template &lt;typename T&gt;
void F(T arg) { ... }
</pre>

<p>In a lambda expression, if you need to refer to the type of a parameter, prefer to make that
type a template parameter:</p>
<pre class="badcode">[](auto x) { decltype(x) y; ... }
</pre>
<pre class="goodcode">[]&lt;typename T&gt;(T x) { T y; ... }
</pre>

<h4>Lambda init captures</h4>

Expand Down