From 8c4da230fc15e01e0ba3545c8a6ff55bc3da4649 Mon Sep 17 00:00:00 2001
From: Daniel Cheng On Unsigned Integers
representing bitfields or modular arithmetic). Do not use an unsigned
type merely to assert that a variable is non-negative.
Code should be 64-bit and 32-bit friendly. Bear in mind -problems of printing, comparisons, and structure alignment.
- -Correct portable printf()
conversion specifiers for
- some integral typedefs rely on macro expansions that we find unpleasant to
- use and impractical to require (the PRI
macros from
- <cinttypes>
). Unless there is no reasonable alternative
- for your particular case, try to avoid or even upgrade APIs that rely on the
- printf
family. Instead use a library supporting typesafe numeric
- formatting, such as
-
-
- StrCat
-
- or
+
Of the built-in C++ floating-point types, the only ones used
+ are float
and
+double
. You may assume that these types represent IEEE-754 binary32
+and binary64, respectively.
Do not use long double
, as it gives non-portable
+results.
Substitute
+
+Write architecture-portable code. Do not rely on CPU features specific to a +single processor.
- orstd::ostream
.
-
- Unfortunately, the PRI
macros are the only portable way to
- specify a conversion for the standard bitwidth typedefs (e.g.,
- int64_t
, uint64_t
, int32_t
,
- uint32_t
, etc).
- Where possible, avoid passing arguments of types specified by bitwidth
- typedefs to printf
-based APIs. Note that it is acceptable
- to use typedefs for which printf has dedicated length modifiers, such as
- size_t
(z
),
- ptrdiff_t
(t
), and
- maxint_t
(j
).
absl::StrCat
,
+ absl::Substitute
,
+ absl::StrFormat
,
+ or std::ostream
instead of the
+ printf
family of functions.sizeof(void *)
!=
- sizeof(int)
. Use intptr_t
if
- you want a pointer-sized integer.int64_t
/uint64_t
- member will by default end up being 8-byte aligned on a
- 64-bit system. If you have such structures being shared
- on disk between 32-bit and 64-bit code, you will need
- to ensure that they are packed the same on both
- architectures.
- Most compilers offer a way to
- alter structure alignment. For gcc, you can use
- __attribute__((packed))
. MSVC offers
- #pragma pack()
and
- __declspec(align())
.uintptr_t
s rather than uint32_t
s or
+ uint64_t
s.Use braced-initialization as needed to create - 64-bit constants. For example:
+ Use braced-initialization as needed to create + 64-bit constants. For example:int64_t my_value{0x123456789}; uint64_t my_mask{uint64_t{3} << 48};
long double
.short
, long
, and long long
.Do not use C++20 Modules.
+ +C++20 introduces "modules", a new language feature designed as an
+alternative to textual inclusion of header files. It introduces three
+new keywords to support
+this: module
, export,
+and import
.
+
+
Modules are a big shift in how C++ is written and compiled, and we +are still assessing how they may fit into Google's C++ ecosystem in +the future. Furthermore, they are not currently well-supported by our +build-systems, compilers, and other tooling, and need further +exploration as to the best-practices when writing and using them.
+ + + +Do not use coroutines (yet).
+ +Do not include the <coroutine>
header,
+or use the co_await
, co_yield
,
+or co_return
keywords.
NOTE: this ban is expected to be temporary, while further +guidance is being developed. + +
+Use only approved libraries from the Boost library @@ -3997,11 +4011,11 @@
As with Boost, some modern C++ -extensions encourage coding practices that hamper +library functionality encourages coding practices that hamper readability—for example by removing checked redundancy (such as type names) that may be helpful to readers, or by encouraging template @@ -4010,8 +4024,7 @@
In addition to what's described in the rest of the style -guide, the following C++ features may not be used:
+The following C++ standard library features may not be used:
myusefulclass_test.cc // _unittest and _regtest are deprecated.
C++ files should end in .cc
and header files should end in
-.h
. Files that rely on being textually included at specific points
+
C++ files should have a .cc
filename extension, and header files
+should have a .h
extension. Files that rely on being textually included at specific points
should end in .inc
(see also the section on
self-contained headers).
You may diverge from the rules when dealing with code that does not conform to this style guide.