Skip to content

Commit cada798

Browse files
authored
Merge pull request google#688 from zetafunction/update-styleguide
Update C++ style guide.
2 parents 992ad03 + 93c7bd8 commit cada798

File tree

1 file changed

+88
-69
lines changed

1 file changed

+88
-69
lines changed

cppguide.html

Lines changed: 88 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -204,19 +204,15 @@ <h3 id="Self_contained_Headers">Self-contained Headers</h3>
204204
have <a href="#The__define_Guard">header guards</a> and include all
205205
other headers it needs.</p>
206206

207-
<p>Prefer placing the definitions for template and inline functions in
208-
the same file as their declarations. The definitions of these
209-
constructs must be included into every <code>.cc</code> file that uses
210-
them, or the program may fail to link in some build configurations. If
211-
declarations and definitions are in different files, including the
212-
former should transitively include the latter. Do not move these
213-
definitions to separately included header files (<code>-inl.h</code>);
214-
this practice was common in the past, but is no longer allowed.</p>
215-
216-
<p>As an exception, a template that is explicitly instantiated for
217-
all relevant sets of template arguments, or that is a private
218-
implementation detail of a class, is allowed to be defined in the one
219-
and only <code>.cc</code> file that instantiates the template.</p>
207+
<p>When a header declares inline functions or templates that clients of the
208+
header will instantiate, the inline functions and templates must also have
209+
definitions in the header, either directly or in files it includes. Do not move
210+
these definitions to separately included header (<code>-inl.h</code>) files;
211+
this practice was common in the past, but is no longer allowed. When all
212+
instantiations of a template occur in one <code>.cc</code> file, either because
213+
they're <a href="https://en.cppreference.com/w/cpp/language/class_template#Explicit_instantiation">
214+
explicit</a> or because the definition is accessible to only
215+
the <code>.cc</code> file, the template definition can be kept in that file.</p>
220216

221217
<p>There are rare cases where a file designed to be included is not
222218
self-contained. These are typically intended to be included at unusual
@@ -331,7 +327,7 @@ <h3 id="Forward_Declarations">Forward Declarations</h3>
331327
#include "b.h"
332328
void f(B*);
333329
void f(void*);
334-
void test(D* x) { f(x); } // calls f(B*)
330+
void test(D* x) { f(x); } // Calls f(B*)
335331
</pre>
336332
If the <code>#include</code> was replaced with forward
337333
decls for <code>B</code> and <code>D</code>,
@@ -492,8 +488,8 @@ <h3 id="Names_and_Order_of_Includes">Names and Order of Includes</h3>
492488
#include &lt;vector&gt;
493489

494490
#include "base/basictypes.h"
495-
#include "base/commandlineflags.h"
496491
#include "foo/server/bar.h"
492+
#include "third_party/absl/flags/flag.h"
497493
</pre>
498494

499495
<p><b>Exception:</b></p>
@@ -618,7 +614,7 @@ <h3 id="Namespaces">Namespaces</h3>
618614

619615
<pre>#include "a.h"
620616

621-
ABSL_FLAG(bool, someflag, false, "dummy flag");
617+
ABSL_FLAG(bool, someflag, false, "a flag");
622618

623619
namespace mynamespace {
624620

@@ -869,26 +865,26 @@ <h4>Decision on destruction</h4>
869865
Fundamental types (like pointers and <code>int</code>) are trivially
870866
destructible, as are arrays of trivially destructible types. Note that
871867
variables marked with <code>constexpr</code> are trivially destructible.</p>
872-
<pre>const int kNum = 10; // allowed
868+
<pre>const int kNum = 10; // Allowed
873869

874870
struct X { int n; };
875-
const X kX[] = {{1}, {2}, {3}}; // allowed
871+
const X kX[] = {{1}, {2}, {3}}; // Allowed
876872

877873
void foo() {
878-
static const char* const kMessages[] = {"hello", "world"}; // allowed
874+
static const char* const kMessages[] = {"hello", "world"}; // Allowed
879875
}
880876

881-
// allowed: constexpr guarantees trivial destructor
882-
constexpr std::array&lt;int, 3&gt; kArray = {{1, 2, 3}};</pre>
877+
// Allowed: constexpr guarantees trivial destructor.
878+
constexpr std::array&lt;int, 3&gt; kArray = {1, 2, 3};</pre>
883879
<pre class="badcode">// bad: non-trivial destructor
884880
const std::string kFoo = "foo";
885881

886-
// bad for the same reason, even though kBar is a reference (the
887-
// rule also applies to lifetime-extended temporary objects)
882+
// Bad for the same reason, even though kBar is a reference (the
883+
// rule also applies to lifetime-extended temporary objects).
888884
const std::string&amp; kBar = StrCat("a", "b", "c");
889885

890886
void bar() {
891-
// bad: non-trivial destructor
887+
// Bad: non-trivial destructor.
892888
static std::map&lt;int, int&gt; kData = {{1, 0}, {2, 0}, {3, 0}};
893889
}</pre>
894890

@@ -902,10 +898,10 @@ <h4>Decision on initialization</h4>
902898
<p>Initialization is a more complex topic. This is because we must not only
903899
consider whether class constructors execute, but we must also consider the
904900
evaluation of the initializer:</p>
905-
<pre class="neutralcode">int n = 5; // fine
906-
int m = f(); // ? (depends on f)
907-
Foo x; // ? (depends on Foo::Foo)
908-
Bar y = g(); // ? (depends on g and on Bar::Bar)
901+
<pre class="neutralcode">int n = 5; // Fine
902+
int m = f(); // ? (Depends on f)
903+
Foo x; // ? (Depends on Foo::Foo)
904+
Bar y = g(); // ? (Depends on g and on Bar::Bar)
909905
</pre>
910906

911907
<p>All but the first statement expose us to indeterminate initialization
@@ -918,9 +914,9 @@ <h4>Decision on initialization</h4>
918914
<code>constexpr</code>, too:</p>
919915
<pre>struct Foo { constexpr Foo(int) {} };
920916

921-
int n = 5; // fine, 5 is a constant expression
922-
Foo x(2); // fine, 2 is a constant expression and the chosen constructor is constexpr
923-
Foo a[] = { Foo(1), Foo(2), Foo(3) }; // fine</pre>
917+
int n = 5; // Fine, 5 is a constant expression.
918+
Foo x(2); // Fine, 2 is a constant expression and the chosen constructor is constexpr.
919+
Foo a[] = { Foo(1), Foo(2), Foo(3) }; // Fine</pre>
924920

925921
<p>Constant initialization is always allowed. Constant initialization of
926922
static storage duration variables should be marked with <code>constexpr</code>
@@ -936,22 +932,22 @@ <h4>Decision on initialization</h4>
936932
<p>By contrast, the following initializations are problematic:</p>
937933

938934
<pre class="badcode">// Some declarations used below.
939-
time_t time(time_t*); // not constexpr!
940-
int f(); // not constexpr!
935+
time_t time(time_t*); // Not constexpr!
936+
int f(); // Not constexpr!
941937
struct Bar { Bar() {} };
942938

943939
// Problematic initializations.
944-
time_t m = time(nullptr); // initializing expression not a constant expression
945-
Foo y(f()); // ditto
946-
Bar b; // chosen constructor Bar::Bar() not constexpr</pre>
940+
time_t m = time(nullptr); // Initializing expression not a constant expression.
941+
Foo y(f()); // Ditto
942+
Bar b; // Chosen constructor Bar::Bar() not constexpr.</pre>
947943

948944
<p>Dynamic initialization of nonlocal variables is discouraged, and in general
949945
it is forbidden. However, we do permit it if no aspect of the program depends
950946
on the sequencing of this initialization with respect to all other
951947
initializations. Under those restrictions, the ordering of the initialization
952948
does not make an observable difference. For example:</p>
953-
<pre>int p = getpid(); // allowed, as long as no other static variable
954-
// uses p in its own initialization</pre>
949+
<pre>int p = getpid(); // Allowed, as long as no other static variable
950+
// uses p in its own initialization.</pre>
955951

956952
<p>Dynamic initialization of static local variables is allowed (and common).</p>
957953

@@ -969,19 +965,20 @@ <h4>Common patterns</h4>
969965
<li>Maps, sets, and other dynamic containers: if you require a static, fixed
970966
collection, such as a set to search against or a lookup table, you cannot
971967
use the dynamic containers from the standard library as a static variable,
972-
since they have non-trivial destructors. Instead, consider a simple array of
973-
trivial types, e.g., an array of arrays of ints (for a "map from int to
968+
since they have non-trivial destructors. Instead, consider
969+
970+
971+
a simple array of trivial types, e.g., an array of arrays of ints (for a "map from int to
974972
int"), or an array of pairs (e.g., pairs of <code>int</code> and <code>const
975973
char*</code>). For small collections, linear search is entirely sufficient
976974
(and efficient, due to memory locality); consider using the facilities from
977-
978975
<a href="https://github.com/abseil/abseil-cpp/blob/master/absl/algorithm/container.h">absl/algorithm/container.h</a>
979-
980-
981976
for the standard operations. If necessary, keep the collection in sorted
982-
order and use a binary search algorithm. If you do really prefer a dynamic
983-
container from the standard library, consider using a function-local static
984-
pointer, as described below.</li>
977+
order and use a binary search algorithm.
978+
979+
If you do really prefer a dynamic container from the standard library, consider using
980+
a function-local static pointer, as described below
981+
.</li>
985982
<li>Smart pointers (<code>unique_ptr</code>, <code>shared_ptr</code>): smart
986983
pointers execute cleanup during destruction and are therefore forbidden.
987984
Consider whether your use case fits into one of the other patterns described
@@ -991,8 +988,8 @@ <h4>Common patterns</h4>
991988
a type that you need to define yourself, give the type a trivial destructor
992989
and a <code>constexpr</code> constructor.</li>
993990
<li>If all else fails, you can create an object dynamically and never delete
994-
it by using a function-local static pointer or reference (e.g., <code>static
995-
const auto&amp; impl = *new T(args...);</code>).</li>
991+
it by using a function-local static pointer or reference (e.g.,
992+
<code>static const auto&amp; impl = *new T(args...);</code>).</li>
996993
</ul>
997994

998995
<h3 id="thread_local">thread_local Variables</h3>
@@ -1705,11 +1702,27 @@ <h3 id="Declaration_Order">Declaration Order</h3>
17051702

17061703
<p>Within each section, prefer grouping similar
17071704
kinds of declarations together, and prefer the
1708-
following order: types and type aliases (<code>typedef</code>,
1709-
<code>using</code>, <code>enum</code>, nested structs and classes),
1710-
static constants, factory functions, constructors and assignment
1711-
operators, destructor, all other member and <code>friend</code> functions,
1712-
data members.</p>
1705+
following order:</p>
1706+
1707+
<ol>
1708+
<li>Types and type aliases (<code>typedef</code>, <code>using</code>,
1709+
<code>enum</code>, nested structs and classes)</li>
1710+
1711+
<li>Static constants</li>
1712+
1713+
<li>Factory functions</li>
1714+
1715+
<li>Constructors and assignment operators</li>
1716+
1717+
<li>Destructor</li>
1718+
1719+
<li>
1720+
All other functions (<code>static</code> and non-<code>static</code> member
1721+
functions, and <code>friend</code> functions)
1722+
</li>
1723+
1724+
<li>Data members (static and non-static)</li>
1725+
</ol>
17131726

17141727
<p>Do not put large method definitions inline in the
17151728
class definition. Usually, only trivial or
@@ -2998,7 +3011,7 @@ <h3 id="64-bit_Portability">64-bit Portability</h3>
29983011
<p>Use <a href="#Casting">braced-initialization</a> as needed to create
29993012
64-bit constants. For example:</p>
30003013
<pre>int64_t my_value{0x123456789};
3001-
uint64_t my_mask{3ULL &lt;&lt; 48};
3014+
uint64_t my_mask{uint64_t{3} &lt;&lt; 48};
30023015
</pre>
30033016
</li>
30043017
</ul>
@@ -3077,6 +3090,8 @@ <h3 id="Preprocessor_Macros">Preprocessor Macros</h3>
30773090

30783091
<li>Prefer not using <code>##</code> to generate
30793092
function/class/variable names.</li>
3093+
3094+
30803095
</ul>
30813096

30823097
<p>Exporting macros from headers (i.e., defining them in a header
@@ -3094,12 +3109,6 @@ <h3 id="0_and_nullptr/NULL">0 and nullptr/NULL</h3>
30943109
<p>For pointers (address values), use <code>nullptr</code>, as this
30953110
provides type-safety.</p>
30963111

3097-
<p>For C++03 projects, prefer <code>NULL</code> to <code>0</code>. While the
3098-
values are equivalent, <code>NULL</code> looks more like a pointer to the
3099-
reader, and some C++ compilers provide special definitions of <code>NULL</code>
3100-
which enable them to give useful warnings. Never use <code>NULL</code> for
3101-
numeric (integer or floating-point) values.</p>
3102-
31033112
<p>Use <code>'\0'</code> for the null character. Using the correct type makes
31043113
the code more readable.</p>
31053114

@@ -3367,7 +3376,7 @@ <h4>Structured bindings</h4>
33673376
field names. We recommend using a comment to indicate the name of the
33683377
underlying field, if it doesn't match the name of the binding, using the
33693378
same syntax as for function parameter comments:</p>
3370-
<pre>auto [/*field_name1=*/ bound_name1, /*field_name2=*/ bound_name2] = ...</pre>
3379+
<pre>auto [/*field_name1=*/bound_name1, /*field_name2=*/bound_name2] = ...</pre>
33713380
<p>As with function parameter comments, this can enable tools to detect if
33723381
you get the order of the fields wrong.</p>
33733382

@@ -4759,8 +4768,20 @@ <h3 id="Line_Length">Line Length</h3>
47594768
readability, ease of cut and paste or auto-linking -- e.g., if a line
47604769
contains an example command or a literal URL longer than 80 characters.</li>
47614770

4762-
<li>a raw-string literal with content that exceeds 80 characters. Except for
4763-
test code, such literals should appear near the top of a file.</li>
4771+
<li>a string literal that cannot easily be wrapped at 80 columns.
4772+
This may be because it contains URIs or other semantically-critical pieces,
4773+
or because the literal contains an embedded language, or a multiline
4774+
literal whose newlines are significant like help messages.
4775+
In these cases, breaking up the literal would
4776+
reduce readability, searchability, ability to click links, etc. Except for
4777+
test code, such literals should appear at namespace scope near the top of a
4778+
file. If a tool like Clang-Format doesn't recognize the unsplittable content,
4779+
<a href="https://clang.llvm.org/docs/ClangFormatStyleOptions.html#disabling-formatting-on-a-piece-of-code">
4780+
disable the tool</a> around the content as necessary.
4781+
<br><br>
4782+
(We must balance between usability/searchability of such literals and the
4783+
readability of the code around them.)
4784+
</li>
47644785

47654786
<li>an include statement.</li>
47664787

@@ -5200,10 +5221,8 @@ <h3 id="Loops_and_Switch_Statements">Loops and Switch Statements</h3>
52005221

52015222
<p>Fall-through from one case label to
52025223
another must be annotated using the
5203-
<code>ABSL_FALLTHROUGH_INTENDED;</code> macro (defined in
5204-
5205-
<code>absl/base/macros.h</code>).
5206-
<code>ABSL_FALLTHROUGH_INTENDED;</code> should be placed at a
5224+
<code>[[fallthrough]];</code> attribute.
5225+
<code>[[fallthrough]];</code> should be placed at a
52075226
point of execution where a fall-through to the next case
52085227
label occurs. A common exception is consecutive case
52095228
labels without intervening code, in which case no
@@ -5214,14 +5233,14 @@ <h3 id="Loops_and_Switch_Statements">Loops and Switch Statements</h3>
52145233
case 43:
52155234
if (dont_be_picky) {
52165235
// Use this instead of or along with annotations in comments.
5217-
ABSL_FALLTHROUGH_INTENDED;
5236+
[[fallthrough]];
52185237
} else {
52195238
CloseButNoCigar();
52205239
break;
52215240
}
52225241
case 42:
52235242
DoSomethingSpecial();
5224-
ABSL_FALLTHROUGH_INTENDED;
5243+
[[fallthrough]];
52255244
default:
52265245
DoSomethingGeneric();
52275246
break;

0 commit comments

Comments
 (0)