blob: 17f17b7d7fc1c9182e7a6fddef7006caeca38fdd [file] [log] [blame] [view]
brettwf0e606a52016-07-06 21:17:201# Chromium C++ style guide
2
Peter Kasting777e1302020-06-02 21:44:403_For other languages, please see the
John Palmerbe051302021-05-19 11:48:354[Chromium style guides](https://chromium.googlesource.com/chromium/src/+/main/styleguide/styleguide.md)._
brettwf0e606a52016-07-06 21:17:205
6Chromium follows the [Google C++ Style
7Guide](https://google.github.io/styleguide/cppguide.html) unless an exception
8is listed below.
9
10A checkout should give you
John Palmerbe051302021-05-19 11:48:3511[clang-format](https://chromium.googlesource.com/chromium/src/+/main/docs/clang_format.md)
brettwf0e606a52016-07-06 21:17:2012to automatically format C++ code. By policy, Clang's formatting of code should
13always be accepted in code reviews.
14
brettw196290a2016-07-07 03:52:1615You can propose changes to this style guide by sending an email to
16`[email protected]`. Ideally, the list will arrive at some consensus and you can
17request review for a change to this file. If there's no consensus,
Peter Kasting0eca431d2024-12-18 19:41:4518`src/styleguide/c++/OWNERS` get to decide. For further details on how style
19changes are handled and communicated, see the C++ Style Changes
20[process documentation](https://chromium.googlesource.com/chromium/src/+/main/docs/process/c++_style_changes.md).
brettwf0e606a52016-07-06 21:17:2021
Tom McKeebfea26782020-02-18 20:57:4722Blink code in `third_party/blink` uses [Blink style](blink-c++.md).
brettwf0e606a52016-07-06 21:17:2023
Jeremy Roman1bebbea2019-06-20 19:17:1424## Modern C++ features
brettwf0e606a52016-07-06 21:17:2025
Peter Kasting1865f2772021-12-23 21:23:5826Google and Chromium style
Jan Wilken Dörrie276003c2023-11-27 16:59:5727[targets C++20](https://google.github.io/styleguide/cppguide.html#C++_Version).
Peter Kasting1865f2772021-12-23 21:23:5828Additionally, some features of supported C++ versions remain forbidden. The
29status of Chromium's C++ support is covered in more detail in
Avi Drissman0aafa9e2022-01-18 21:41:0130[Modern C++ use in Chromium](c++-features.md).
brettwf0e606a52016-07-06 21:17:2031
32## Naming
33
34 * "Chromium" is the name of the project, not the product, and should never
35 appear in code, variable names, API names etc. Use "Chrome" instead.
36
Dominic Farolinof3d03072021-07-20 15:55:5437## Tests and Test-only Code
Tommy C. Li1a13764a2018-09-17 21:18:3438
39 * Functions used only for testing should be restricted to test-only usages
Peter Kasting777e1302020-06-02 21:44:4040 with the testing suffixes supported by
Joseph Koshy0b38a322022-07-13 15:01:4041 [PRESUBMIT.py](https://chromium.googlesource.com/chromium/src/+/main/PRESUBMIT.py).
Caitlin Fischer210cfab2020-05-07 20:04:3042 `ForTesting` is the conventional suffix although similar patterns, such as
43 `ForTest`, are also accepted. These suffixes are checked at presubmit time
Sam Fortiner3b827bd2023-11-27 21:42:0144 to ensure the functions are called only by test files. In the rare case of
45 adding a test-only code path to an area where a testing suffix is not
46 possible, CHECK_IS_TEST() may be appropriate.
Roland Bock1f21a732021-05-20 16:59:5347 * Classes used only for testing should be in a GN build target that is
48 marked `testonly=true`. Tests can depend on such targets, but production
49 code can not.
Dominic Farolino200a14ec2021-07-20 16:42:4050 * While test files generally appear alongside the production code they test,
51 support code for `testonly` targets should be placed in a `test/` subdirectory.
52 For example, see `//mojo/core/core_unittest.cc` and
53 `//mojo/core/test/mojo_test_base.cc`. For test classes used across multiple
54 directories, it might make sense to move them into a nested `test` namespace for
55 clarity.
Dominic Farolinof3d03072021-07-20 15:55:5456 * Despite the Google C++ style guide
57 [deprecating](https://google.github.io/styleguide/cppguide.html#File_Names)
58 the `_unittest.cc` suffix for unit test files, in Chromium we still use this
59 suffix to distinguish unit tests from browser tests, which are written in
60 files with the `_browsertest.cc` suffix.
Tommy C. Li1a13764a2018-09-17 21:18:3461
brettwf0e606a52016-07-06 21:17:2062## Code formatting
63
64 * Put `*` and `&` by the type rather than the variable name.
danakjcd463f12021-02-12 19:53:1365 * In class declarations, group function overrides together within each access
Peter Kasting5e04bd32019-05-21 21:44:1066 control section, with one labeled group per parent class.
brettwf0e606a52016-07-06 21:17:2067 * Prefer `(foo == 0)` to `(0 == foo)`.
Nicholas Bishop27098a12024-01-16 20:41:5568 * Use `{}` on all conditionals/loops.
brettwf0e606a52016-07-06 21:17:2069
brettwf0e606a52016-07-06 21:17:2070## Unnamed namespaces
71
72Items local to a .cc file should be wrapped in an unnamed namespace. While some
73such items are already file-scope by default in C++, not all are; also, shared
74objects on Linux builds export all symbols, so unnamed namespaces (which
75restrict these symbols to the compilation unit) improve function call cost and
76reduce the size of entry point tables.
77
78## Exporting symbols
79
Peter Kasting5e04bd32019-05-21 21:44:1080Symbols can be exported (made visible outside of a shared library/DLL) by
81annotating with a `<COMPONENT>_EXPORT` macro name (where `<COMPONENT>` is the
82name of the component being built, e.g. BASE, NET, CONTENT, etc.). Class
83annotations should precede the class name:
brettwf0e606a52016-07-06 21:17:2084```c++
85class FOO_EXPORT Foo {
86 void Bar();
87 void Baz();
88 // ...
89};
90```
91
92Function annotations should precede the return type:
93```c++
94class FooSingleton {
95 FOO_EXPORT Foo& GetFoo();
Jeremy Roman312202882019-02-19 21:43:5096 FOO_EXPORT Foo& SetFooForTesting(Foo* foo);
Peter Kasting5e04bd32019-05-21 21:44:1097 void SetFoo(Foo* foo); // Not exported.
brettwf0e606a52016-07-06 21:17:2098};
99```
100
brettwf0e606a52016-07-06 21:17:20101## Multiple inheritance
102
103Multiple inheritance and virtual inheritance are permitted in Chromium code,
104but discouraged (beyond the "interface" style of inheritance allowed by the
105Google style guide, for which we do not require classes to have the "Interface"
106suffix). Consider whether composition could solve the problem instead.
107
108## Inline functions
109
110Simple accessors should generally be the only inline functions. These should be
Peter Kasting5e04bd32019-05-21 21:44:10111named using `snake_case()`. Virtual functions should never be declared this way.
brettwf0e606a52016-07-06 21:17:20112
113## Logging
114
Erik Chencd92bd22024-02-02 04:09:54115Remove all logging before checking in code. The exception is temporary logging
116to track down a specific bug. This should be a rare exception, and you should
117have a plan for how to manually collect/use the logged data. Afterwards you
118should remove the logging. Note that logs are not present in crashes. Use
119`base::debug::ScopedCrashKeyString`
120([link](https://chromium.googlesource.com/chromium/src/+/main/base/debug/crash_logging.h))
121for that.
brettwf0e606a52016-07-06 21:17:20122
123For the rare case when logging needs to stay in the codebase for a while,
124prefer `DVLOG(1)` to other logging methods. This avoids bloating the release
125executable and in debug can be selectively enabled at runtime by command-line
126arguments:
Peter Kasting5e04bd32019-05-21 21:44:10127 * `--v=n` sets the global log level to n (default 0). All log statements with
128 a log level less than or equal to the global level will be printed.
brettwf0e606a52016-07-06 21:17:20129 * `--vmodule=mod=n[,mod=n,...]` overrides the global log level for the module
130 mod. Supplying the string foo for mod will affect all files named foo.cc,
131 while supplying a wildcard like `*bar/baz*` will affect all files with
132 `bar/baz` in their full pathnames.
133
Erik Chencd92bd22024-02-02 04:09:54134Rationale:
135* Logging is expensive: binary size, runtime.
136* Logging quickly loses utility as more components emit logs: too much noise,
137 not enough signal.
138* Logging is often used to document impossible edge cases which should be
139 enforced with CHECKs. The latter makes it easier to reason about the code, and
140 can result in more performant binaries.
141
brettwf0e606a52016-07-06 21:17:20142## Platform-specific code
143
144To `#ifdef` code for specific platforms, use the macros defined in
145`build/build_config.h` and in the Chromium build config files, not other macros
Tommy C. Li1a13764a2018-09-17 21:18:34146set by specific compilers or build environments (e.g. `WIN32`).
brettwf0e606a52016-07-06 21:17:20147
148Place platform-specific #includes in their own section below the "normal"
149`#includes`. Repeat the standard `#include` order within this section:
150
151```c++
152 #include "foo/foo.h"
153
154 #include <stdint.h>
155 #include <algorithm>
156
157 #include "base/strings/utf_string_conversions.h"
Xiaohan Wang0727d882022-01-21 03:03:43158 #include "build/build_config.h"
brettwf0e606a52016-07-06 21:17:20159 #include "chrome/common/render_messages.h"
160
Xiaohan Wang0727d882022-01-21 03:03:43161 #if BUILDFLAG(IS_WIN)
brettwf0e606a52016-07-06 21:17:20162 #include <windows.h>
Robert Liaoc7c9a1c2017-10-18 01:41:31163 #include "base/win/com_init_util.h"
Xiaohan Wang0727d882022-01-21 03:03:43164 #elif BUILDFLAG(IS_POSIX)
brettwf0e606a52016-07-06 21:17:20165 #include "base/posix/global_descriptors.h"
166 #endif
167```
168
169## Types
170
Dominick Ng71a699a2022-01-26 23:46:57171 * Refer to the [Mojo style
172 guide](https://chromium.googlesource.com/chromium/src/+/main/docs/security/mojo.md)
173 when working with types that will be passed across network or process
174 boundaries. For example, explicitly-sized integral types must be used for
175 safety, since the sending and receiving ends may not have been compiled
176 with the same sizes for things like `int` and `size_t`.
brettwf0e606a52016-07-06 21:17:20177 * Use `size_t` for object and allocation sizes, object counts, array and
Peter Kasting5e04bd32019-05-21 21:44:10178 pointer offsets, vector indices, and so on. This prevents casts when
179 dealing with STL APIs, and if followed consistently across the codebase,
180 minimizes casts elsewhere.
181 * Occasionally classes may have a good reason to use a type other than
182 `size_t` for one of these concepts, e.g. as a storage space optimization. In
183 these cases, continue to use `size_t` in public-facing function
184 declarations, and continue to use unsigned types internally (e.g.
185 `uint32_t`).
Dominick Ng71a699a2022-01-26 23:46:57186 * Follow the [integer semantics
187 guide](https://chromium.googlesource.com/chromium/src/+/main/docs/security/integer-semantics.md)
188 for all arithmetic conversions and calculations used in memory management
189 or passed across network or process boundaries. In other circumstances,
190 follow [Google C++ casting
Dana Fried74a18892018-09-13 19:10:01191 conventions](https://google.github.io/styleguide/cppguide.html#Casting)
192 to convert arithmetic types when you know the conversion is safe. Use
Chris Palmer8218b8572019-02-26 00:19:16193 `checked_cast<T>` (from `base/numerics/safe_conversions.h`) when you need to
194 `CHECK` that the source value is in range for the destination type. Use
195 `saturated_cast<T>` if you instead wish to clamp out-of-range values.
Peter Kasting5e04bd32019-05-21 21:44:10196 `CheckedNumeric` is an ergonomic way to perform safe arithmetic and casting
197 in many cases.
Peter Kasting9e210a522021-03-20 15:45:42198 * The Google Style Guide [bans
199 UTF-16](https://google.github.io/styleguide/cppguide.html#Non-ASCII_Characters).
200 For various reasons, Chromium uses UTF-16 extensively. Use `std::u16string`
201 and `char16_t*` for 16-bit strings, `u"..."` to declare UTF-16 literals, and
202 either the actual characters or the `\uXXXX` or `\UXXXXXXXX` escapes for
203 Unicode characters. Avoid `\xXX...`-style escapes, which can cause subtle
204 problems if someone attempts to change the type of string that holds the
205 literal. In code used only on Windows, it may be necessary to use
206 `std::wstring` and `wchar_t*`; these are legal, but note that they are
207 distinct types and are often not 16-bit on other platforms.
brettwf0e606a52016-07-06 21:17:20208
209## Object ownership and calling conventions
210
211When functions need to take raw or smart pointers as parameters, use the
212following conventions. Here we refer to the parameter type as `T` and name as
213`t`.
Peter Kasting5e04bd32019-05-21 21:44:10214 * If the function does not modify `t`'s ownership, declare the param as `T*`.
215 The caller is expected to ensure `t` stays alive as long as necessary,
216 generally through the duration of the call. Exception: In rare cases (e.g.
217 using lambdas with STL algorithms over containers of `unique_ptr<>`s), you
218 may be forced to declare the param as `const std::unique_ptr<T>&`. Do this
219 only when required.
brettwf0e606a52016-07-06 21:17:20220 * If the function takes ownership of a non-refcounted object, declare the
221 param as `std::unique_ptr<T>`.
brettwf0e606a52016-07-06 21:17:20222 * If the function (at least sometimes) takes a ref on a refcounted object,
223 declare the param as `scoped_refptr<T>`. The caller can decide
224 whether it wishes to transfer ownership (by calling `std::move(t)` when
225 passing `t`) or retain its ref (by simply passing t directly).
brettwf0e606a52016-07-06 21:17:20226 * In short, functions should never take ownership of parameters passed as raw
227 pointers, and there should rarely be a need to pass smart pointers by const
228 ref.
229
Gabriel Charetteb62806f4a2019-01-29 21:08:41230Conventions for return values are similar with an important distinction:
231 * Return raw pointers if-and-only-if the caller does not take ownership.
232 * Return `std::unique_ptr<T>` or `scoped_refptr<T>` by value when the impl is
233 handing off ownership.
234 * **Distinction**: Return `const scoped_refptr<T>&` when the impl retains
235 ownership so the caller isn't required to take a ref: this avoids bumping
236 the reference count if the caller doesn't need ownership and also
237 [helps binary size](https://crrev.com/c/1435627)).
brettwf0e606a52016-07-06 21:17:20238
239A great deal of Chromium code predates the above rules. In particular, some
240functions take ownership of params passed as `T*`, or take `const
241scoped_refptr<T>&` instead of `T*`, or return `T*` instead of
242`scoped_refptr<T>` (to avoid refcount churn pre-C++11). Try to clean up such
243code when you find it, or at least not make such usage any more widespread.
244
Lukasz Anforowicz269e41d2021-11-30 18:32:01245## Non-owning pointers in class fields
246
danakj2c782052022-06-30 02:13:48247Use `const raw_ref<T>` or `raw_ptr<T>` for class and struct fields in place of a
248raw C++ reference `T&` or pointer `T*` whenever possible, except in paths that include
Orr Bernstein41ce8f02022-12-20 17:09:39249`/renderer/` or `blink/public/web/`. These are non-owning smart pointers that
danakj2c782052022-06-30 02:13:48250have improved memory-safety over raw pointers and references, and can prevent
251exploitation of a significant percentage of Use-after-Free bugs.
Lukasz Anforowicz269e41d2021-11-30 18:32:01252
danakj2c782052022-06-30 02:13:48253Prefer `const raw_ref<T>` whenever the held pointer will never be null, and it's
254ok to drop the `const` if the internal reference can be reassigned to point to a
255different `T`. Use `raw_ptr<T>` in order to express that the pointer _can_ be
256null. Only `raw_ptr<T>` can be default-constructed, since `raw_ref<T>` disallows
257nullness.
258
259Using `raw_ref<T>` or `raw_ptr<T>` may not be possible in rare cases for
260[performance reasons](../../base/memory/raw_ptr.md#Performance). Additionally,
261`raw_ptr<T>` doesn’t support some C++ scenarios (e.g. `constexpr`, ObjC
262pointers). Tooling will help to encourage use of these types in the future. See
263[raw_ptr.md](../../base/memory/raw_ptr.md#When-to-use-raw_ptr_T) for how to add
264exclusions.
Lukasz Anforowicz269e41d2021-11-30 18:32:01265
Peter Kasting618227f2023-03-02 02:36:42266## thread_local variables
267
268Much code in Chrome needs to be "sequence-aware" rather than "thread-aware". If
269you need a sequence-local variable, see
270[`base::SequenceLocalStorageSlot`](../../base/threading/sequence_local_storage_slot.h).
271
272If you truly need a thread-local variable, then you can use a `thread_local`, as
273long as it complies with the following requirements:
Peter Kastingc242b102023-07-27 20:39:12274 * Its type must satisfy `std::is_trivially_destructible_v<T>`, due to past
275 problems with "spooky action at a distance" during destruction. Note that
276 `raw_ptr<T>` is not a trivially-destructible type and may not be contained
277 in `thread_locals`.
Peter Kasting618227f2023-03-02 02:36:42278 * It must not be exported (e.g. via `COMPONENT_EXPORT`), since this may result
279 in codegen bugs on Mac; and at least on Windows, this probably won't compile
280 in the component build anyway. As a workaround, create an exported getter
281 function that creates a `thread_local` internally and returns a ref to it.
282 * If it lives at class/namespace scope, it must be marked `ABSL_CONST_INIT`,
283 as specified in
284 [the Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html#thread_local).
285 * It must not be constructed inside OOM handlers or any other code that cannot
286 allocate memory, since on POSIX, construction may alloc.
Peter Kasting4f398232023-03-16 01:34:24287
Peter Kasting618227f2023-03-02 02:36:42288If you can't comply with these requirements, consider
289[`base::ThreadLocalOwnedPointer`](../../base/threading/thread_local.h) or
290another nearby low-level utility.
291
brettwf0e606a52016-07-06 21:17:20292## Forward declarations vs. #includes
293
294Unlike the Google style guide, Chromium style prefers forward declarations to
295`#includes` where possible. This can reduce compile times and result in fewer
296files needing recompilation when a header changes.
297
298You can and should use forward declarations for most types passed or returned
299by value, reference, or pointer, or types stored as pointer members or in most
300STL containers. However, if it would otherwise make sense to use a type as a
301member by-value, don't convert it to a pointer just to be able to
302forward-declare the type.
303
Joe Mason83d069d2024-10-23 17:15:17304Headers that contain only forward declarations, such as
305[`callback_forward.h`](../../base/functional/callback_forward.h), satisfy the
306spirit of this rule. Note that the [Mojo bindings
307generator](../../mojo/public/cpp/bindings/README.md#Getting-Started)
308creates a `.mojom-forward.h` file along with every generated `.mojom.h` file
309that can be included for forward declarations of Mojo types.
310
Erik Staab5c497392025-02-11 19:38:19311See [these tips](c++-dos-and-donts.md#minimize-code-in-headers) for more advice
312on minimizing code in headers.
313
brettwf0e606a52016-07-06 21:17:20314## File headers
315
Peter Kasting5e04bd32019-05-21 21:44:10316All files in Chromium start with a common license header. That header should
317look like this:
brettwf0e606a52016-07-06 21:17:20318
319```c++
Avi Drissman7b017a992022-09-07 15:50:38320// Copyright $YEAR The Chromium Authors
brettwf0e606a52016-07-06 21:17:20321// Use of this source code is governed by a BSD-style license that can be
322// found in the LICENSE file.
323```
324
325Some important notes about this header:
Peter Kasting5e04bd32019-05-21 21:44:10326 * `$YEAR` should be set to the current year at the time a file is created, and
327 not changed thereafter.
Tommy Chiang174366f2022-07-27 16:40:53328 * For files specific to ChromiumOS, replace the word Chromium with the phrase
329 ChromiumOS.
brettwf0e606a52016-07-06 21:17:20330 * The Chromium project hosts mirrors of some upstream open-source projects.
331 When contributing to these portions of the repository, retain the existing
332 file headers.
333
334Use standard `#include` guards in all header files (see the Google style guide
335sections on these for the naming convention). Do not use `#pragma once`;
336historically it was not supported on all platforms, and it does not seem to
337outperform #include guards even on platforms which do support it.
338
Peter Boström006c93b2024-08-06 00:38:39339## CHECK(), DCHECK() and NOTREACHED()
brettwf0e606a52016-07-06 21:17:20340
Peter Boström835c77052023-03-03 21:33:23341Use the `CHECK()` family of macros to both document and verify invariants.
342 * Exception: If the invariant is known to be too expensive to verify in
343 production, you may fall back to `DCHECK()`. Do not do this unless
344 necessary.
Peter Boström25052092023-12-09 00:27:24345 * Exception: If your pre-stable coverage is too small to prevent a stability
346 risk once `CHECK()`s hit stable, and failure doesn't obviously result in a
347 crash or security risk, you may use `CHECK(Foo(),
348 base::NotFatalUntil::M120)` with a future milestone to gather non-fatal
349 diagnostics in stable before automatically turning fatal in a later
350 milestone.
Peter Boström835c77052023-03-03 21:33:23351 * Historically, Chromium code used `DCHECK()` in most cases, so a great deal
Vincent Scheibd4a8acc2023-03-16 23:00:07352 of existing code uses `DCHECK()` instead of `CHECK()`. You are encouraged
Peter Boström25052092023-12-09 00:27:24353 to migrate to `CHECK()`s with a trailing `base::NotFatalUntil::M120`
354 argument, as there's stability risk given the under-tested invariant, or add
355 a comment explaining why DCHECK is appropriate given the current guidance.
brettwf0e606a52016-07-06 21:17:20356
Peter Boström006c93b2024-08-06 00:38:39357Use `NOTREACHED()` to indicate a piece of code is unreachable. Control flow does
358not leave this call, so there should be no executable statements after it (even
359return statements from non-void functions). The compiler will issue dead-code
360warnings.
Peter Boström835c77052023-03-03 21:33:23361 * Prefer to unconditionally `CHECK()` instead of conditionally hitting a
Peter Boström006c93b2024-08-06 00:38:39362 `NOTREACHED()`, where feasible.
Peter Boström25052092023-12-09 00:27:24363 * Exception: If your pre-stable coverage is too small to prevent a stability
Peter Boström006c93b2024-08-06 00:38:39364 risk once `NOTREACHED()`s hit stable, and failure doesn't obviously
Peter Boström25052092023-12-09 00:27:24365 result in a crash or security risk, you may use `NOTREACHED(
366 base::NotFatalUntil::M120)` with a future milestone to gather non-fatal
367 diagnostics in stable before automatically turning fatal in a later
368 milestone.
Peter Boström835c77052023-03-03 21:33:23369
370Use `base::ImmediateCrash()` in the rare case where it's necessary to terminate
371the current process for reasons outside its control, that are not violations of
372our invariants.
373
374Use `base::debug::DumpWithoutCrashing()` to generate a crash report but keep
375running in the case where you are investigating some failure but know that it's
376safe to continue execution.
377
378Use `DLOG(FATAL)` (does nothing in production) or `LOG(DFATAL)` (logs an error
379and continues running in production) if you need to log an error in tests from
380production code. From test code, use `ADD_FAILURE()` directly. Do not use these
Peter Boström006c93b2024-08-06 00:38:39381for invariant failures. Those should use `CHECK()` or `NOTREACHED()` as noted
382above.
Peter Boström835c77052023-03-03 21:33:23383
384For more details, see [checks.md](checks.md).
brettwf0e606a52016-07-06 21:17:20385
Roland Bockd662a2d2022-07-12 20:55:27386## Test-only code paths in production code
387
388Try to avoid test-only code paths in production code. Such code paths make
389production code behave differently in tests. This makes both tests and
390production code hard to reason about. Consider dependency injection, fake
391classes, etc to avoid such code paths.
392
393However, if a test-only path in production code cannot be avoided, instrument
394that code path with `CHECK_IS_TEST();` to assert that the code is only run in
395tests.
396
397```c++
398// `profile_manager` may not be available in tests.
399if (!profile_manager) {
400 CHECK_IS_TEST();
401 return std::string();
402}
403```
404
405`CHECK_IS_TEST();` will crash outside of tests. This asserts that the test-only
406code path is not accidentally or maliciously taken in production.
407
brettwf0e606a52016-07-06 21:17:20408## Miscellany
409
410 * Use UTF-8 file encodings and LF line endings.
brettwf0e606a52016-07-06 21:17:20411 * Unit tests and performance tests should be placed in the same directory as
412 the functionality they're testing.
Xiaohan Wangcc517f082019-01-16 01:58:14413 * The [C++ Dos and Don'ts](c++-dos-and-donts.md) page has more helpful
414 information.