Skip to content

Commit 209d381

Browse files
authored
Merge pull request google#328 from pwnall/update_cpp_2
Minor updates to C++ style guide.
2 parents a176781 + 4b9c0c0 commit 209d381

File tree

1 file changed

+89
-12
lines changed

1 file changed

+89
-12
lines changed

cppguide.html

Lines changed: 89 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,14 @@ <h3 id="Namespaces">Namespaces</h3>
640640
</pre>
641641
</li>
642642

643+
<li>To place generated protocol
644+
message code in a namespace, use the
645+
<code>package</code> specifier in the
646+
<code>.proto</code> file. See
643647

648+
<a href="https://developers.google.com/protocol-buffers/docs/reference/cpp-generated#package">
649+
Protocol Buffer Packages</a>
650+
for details.</li>
644651

645652
<li>Do not declare anything in namespace
646653
<code>std</code>, including forward declarations of
@@ -955,7 +962,11 @@ <h4>Decision on initialization</h4>
955962

956963
<p>Constant initialization is always allowed. Constant initialization of
957964
static storage duration variables should be marked with <code>constexpr</code>
958-
where possible. Any non-local static storage
965+
or where possible the
966+
967+
<a href="https://github.com/abseil/abseil-cpp/blob/03c1513538584f4a04d666be5eb469e3979febba/absl/base/attributes.h#L540">
968+
<code>ABSL_CONST_INIT</code></a>
969+
attribute. Any non-local static storage
959970
duration variable that is not so marked should be presumed to have
960971
dynamic initialization, and reviewed very carefully.</p>
961972

@@ -1021,7 +1032,12 @@ <h3 id="thread_local">thread_local Variables</h3>
10211032

10221033
<div class="summary">
10231034
<p><code>thread_local</code> variables that aren't declared inside a function
1024-
must be initialized with a true compile-time constant. Prefer
1035+
must be initialized with a true compile-time constant,
1036+
and this must be enforced by using the
1037+
1038+
<a href="https://github.com/abseil/abseil-cpp/blob/master/absl/base/attributes.h">
1039+
<code>ABSL_CONST_INIT</code></a>
1040+
attribute. Prefer
10251041
<code>thread_local</code> over other ways of defining thread-local data.</p>
10261042
</div>
10271043

@@ -1093,9 +1109,16 @@ <h3 id="thread_local">thread_local Variables</h3>
10931109

10941110
<p><code>thread_local</code> variables at class or namespace scope must be
10951111
initialized with a true compile-time constant (i.e. they must have no
1096-
dynamic initialization). </p>
1097-
1098-
1112+
dynamic initialization). To enforce this,
1113+
<code>thread_local</code> variables at class or namespace scope must be
1114+
annotated with
1115+
1116+
<a href="https://github.com/abseil/abseil-cpp/blob/master/absl/base/attributes.h">
1117+
<code>ABSL_CONST_INIT</code></a>
1118+
(or <code>constexpr</code>, but that should be rare):</p>
1119+
1120+
<pre>ABSL_CONST_INIT thread_local Foo foo = ...;
1121+
</pre>
10991122

11001123
<p><code>thread_local</code> should be preferred over other mechanisms for
11011124
defining thread-local data.</p>
@@ -1166,7 +1189,12 @@ <h3 id="Doing_Work_in_Constructors">Doing Work in Constructors</h3>
11661189
,
11671190
terminating the program may be an appropriate error handling
11681191
response. Otherwise, consider a factory function
1169-
or <code>Init()</code> method. Avoid <code>Init()</code> methods on objects with
1192+
or <code>Init()</code> method as described in
1193+
1194+
1195+
<a href="https://abseil.io/tips/42">TotW #42</a>
1196+
.
1197+
Avoid <code>Init()</code> methods on objects with
11701198
no other states that affect which public methods may be called
11711199
(semi-constructed objects of this form are particularly hard to work
11721200
with correctly).</p>
@@ -1226,7 +1254,10 @@ <h3 id="Implicit_Conversions">Implicit Conversions</h3>
12261254
expressive by eliminating the need to explicitly name a type
12271255
when it's obvious.</li>
12281256
<li>Implicit conversions can be a simpler alternative to
1229-
overloading.</li>
1257+
overloading, such as when a single
1258+
function with a <code>string_view</code> parameter takes the
1259+
place of separate overloads for <code>string</code> and
1260+
<code>const char*</code>.</li>
12301261
<li>List initialization syntax is a concise and expressive
12311262
way of initializing objects.</li>
12321263
</ul>
@@ -2924,7 +2955,15 @@ <h3 id="Streams">Streams</h3>
29242955
human-readable, and targeted at other developers rather than
29252956
end-users. Be consistent with the code around you, and with the
29262957
codebase as a whole; if there's an established tool for
2927-
your problem, use that tool instead. </p>
2958+
your problem, use that tool instead.
2959+
In particular,
2960+
logging libraries are usually a better
2961+
choice than <code>std::cerr</code> or <code>std::clog</code>
2962+
for diagnostic output, and the libraries in
2963+
2964+
<code>absl/strings</code>
2965+
or the equivalent are usually a
2966+
better choice than <code>std::stringstream</code>.</p>
29282967

29292968
<p>Avoid using streams for I/O that faces external users or
29302969
handles untrusted data. Instead, find and use the appropriate
@@ -2934,7 +2973,10 @@ <h3 id="Streams">Streams</h3>
29342973
<p>If you do use streams, avoid the stateful parts of the
29352974
streams API (other than error state), such as <code>imbue()</code>,
29362975
<code>xalloc()</code>, and <code>register_callback()</code>.
2937-
Use explicit formatting functions rather than
2976+
Use explicit formatting functions (see e.g.
2977+
2978+
<code>absl/strings</code>)
2979+
rather than
29382980
stream manipulators or formatting flags to control formatting
29392981
details such as number base, precision, or padding.</p>
29402982

@@ -3271,8 +3313,14 @@ <h3 id="64-bit_Portability">64-bit Portability</h3>
32713313
for your particular case, try to avoid or even upgrade APIs that rely on the
32723314
<code>printf</code> family. Instead use a library supporting typesafe numeric
32733315
formatting, such as
3316+
3317+
<a href="https://github.com/abseil/abseil-cpp/blob/master/absl/strings/str_cat.h"><code>StrCat</code></a>
3318+
or
3319+
3320+
<a href="https://github.com/abseil/abseil-cpp/blob/master/absl/strings/substitute.h"><code>Substitute</code></a>
3321+
for fast simple conversions,
32743322

3275-
<a href="#Streams"><code>std::ostream</code></a>.</p>
3323+
or <a href="#Streams"><code>std::ostream</code></a>.</p>
32763324

32773325
<p>Unfortunately, the <code>PRI</code> macros are the only portable way to
32783326
specify a conversion for the standard bitwidth typedefs (e.g.
@@ -3531,7 +3579,8 @@ <h3 id="auto">auto</h3>
35313579
<li>(Allowed) When the type is clear from local context (in the same expression
35323580
or within a few lines). Initialization of a pointer or smart pointer
35333581
with calls
3534-
to <code>new</code>
3582+
to <code>new</code> and
3583+
<code>std::make_unique</code>
35353584
commonly falls into this category, as does use of <code>auto</code> in
35363585
a range-based loop over a container whose type is spelled out
35373586
nearby.</li>
@@ -5760,9 +5809,37 @@ <h3 id="Loops_and_Switch_Statements">Loops and Switch Statements</h3>
57605809
</pre>
57615810
</div>
57625811

5812+
<p>Fall-through from one case label to
5813+
another must be annotated using the
5814+
<code>ABSL_FALLTHROUGH_INTENDED;</code> macro (defined in
57635815

5816+
<code>absl/base/macros.h</code>).
5817+
<code>ABSL_FALLTHROUGH_INTENDED;</code> should be placed at a
5818+
point of execution where a fall-through to the next case
5819+
label occurs. A common exception is consecutive case
5820+
labels without intervening code, in which case no
5821+
annotation is needed.</p>
57645822

5765-
5823+
<div>
5824+
<pre>switch (x) {
5825+
case 41: // No annotation needed here.
5826+
case 43:
5827+
if (dont_be_picky) {
5828+
// Use this instead of or along with annotations in comments.
5829+
ABSL_FALLTHROUGH_INTENDED;
5830+
} else {
5831+
CloseButNoCigar();
5832+
break;
5833+
}
5834+
case 42:
5835+
DoSomethingSpecial();
5836+
ABSL_FALLTHROUGH_INTENDED;
5837+
default:
5838+
DoSomethingGeneric();
5839+
break;
5840+
}
5841+
</pre>
5842+
</div>
57665843

57675844
<p> Braces are optional for single-statement loops.</p>
57685845

0 commit comments

Comments
 (0)