@@ -640,7 +640,14 @@ <h3 id="Namespaces">Namespaces</h3>
640
640
</ pre >
641
641
</ li >
642
642
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
643
647
648
+ < a href ="https://developers.google.com/protocol-buffers/docs/reference/cpp-generated#package ">
649
+ Protocol Buffer Packages</ a >
650
+ for details.</ li >
644
651
645
652
< li > Do not declare anything in namespace
646
653
< code > std</ code > , including forward declarations of
@@ -955,7 +962,11 @@ <h4>Decision on initialization</h4>
955
962
956
963
< p > Constant initialization is always allowed. Constant initialization of
957
964
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
959
970
duration variable that is not so marked should be presumed to have
960
971
dynamic initialization, and reviewed very carefully.</ p >
961
972
@@ -1021,7 +1032,12 @@ <h3 id="thread_local">thread_local Variables</h3>
1021
1032
1022
1033
< div class ="summary ">
1023
1034
< 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
1025
1041
< code > thread_local</ code > over other ways of defining thread-local data.</ p >
1026
1042
</ div >
1027
1043
@@ -1093,9 +1109,16 @@ <h3 id="thread_local">thread_local Variables</h3>
1093
1109
1094
1110
< p > < code > thread_local</ code > variables at class or namespace scope must be
1095
1111
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 >
1099
1122
1100
1123
< p > < code > thread_local</ code > should be preferred over other mechanisms for
1101
1124
defining thread-local data.</ p >
@@ -1166,7 +1189,12 @@ <h3 id="Doing_Work_in_Constructors">Doing Work in Constructors</h3>
1166
1189
,
1167
1190
terminating the program may be an appropriate error handling
1168
1191
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
1170
1198
no other states that affect which public methods may be called
1171
1199
(semi-constructed objects of this form are particularly hard to work
1172
1200
with correctly).</ p >
@@ -1226,7 +1254,10 @@ <h3 id="Implicit_Conversions">Implicit Conversions</h3>
1226
1254
expressive by eliminating the need to explicitly name a type
1227
1255
when it's obvious.</ li >
1228
1256
< 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 >
1230
1261
< li > List initialization syntax is a concise and expressive
1231
1262
way of initializing objects.</ li >
1232
1263
</ ul >
@@ -2924,7 +2955,15 @@ <h3 id="Streams">Streams</h3>
2924
2955
human-readable, and targeted at other developers rather than
2925
2956
end-users. Be consistent with the code around you, and with the
2926
2957
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 >
2928
2967
2929
2968
< p > Avoid using streams for I/O that faces external users or
2930
2969
handles untrusted data. Instead, find and use the appropriate
@@ -2934,7 +2973,10 @@ <h3 id="Streams">Streams</h3>
2934
2973
< p > If you do use streams, avoid the stateful parts of the
2935
2974
streams API (other than error state), such as < code > imbue()</ code > ,
2936
2975
< 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
2938
2980
stream manipulators or formatting flags to control formatting
2939
2981
details such as number base, precision, or padding.</ p >
2940
2982
@@ -3271,8 +3313,14 @@ <h3 id="64-bit_Portability">64-bit Portability</h3>
3271
3313
for your particular case, try to avoid or even upgrade APIs that rely on the
3272
3314
< code > printf</ code > family. Instead use a library supporting typesafe numeric
3273
3315
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,
3274
3322
3275
- < a href ="#Streams "> < code > std::ostream</ code > </ a > .</ p >
3323
+ or < a href ="#Streams "> < code > std::ostream</ code > </ a > .</ p >
3276
3324
3277
3325
< p > Unfortunately, the < code > PRI</ code > macros are the only portable way to
3278
3326
specify a conversion for the standard bitwidth typedefs (e.g.
@@ -3531,7 +3579,8 @@ <h3 id="auto">auto</h3>
3531
3579
< li > (Allowed) When the type is clear from local context (in the same expression
3532
3580
or within a few lines). Initialization of a pointer or smart pointer
3533
3581
with calls
3534
- to < code > new</ code >
3582
+ to < code > new</ code > and
3583
+ < code > std::make_unique</ code >
3535
3584
commonly falls into this category, as does use of < code > auto</ code > in
3536
3585
a range-based loop over a container whose type is spelled out
3537
3586
nearby.</ li >
@@ -5760,9 +5809,37 @@ <h3 id="Loops_and_Switch_Statements">Loops and Switch Statements</h3>
5760
5809
</ pre >
5761
5810
</ div >
5762
5811
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
5763
5815
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 >
5764
5822
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 >
5766
5843
5767
5844
< p > Braces are optional for single-statement loops.</ p >
5768
5845
0 commit comments