You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CppCoreGuidelines.md
+14-22Lines changed: 14 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -1308,6 +1308,14 @@ Non-`const` global variables hide dependencies and make the dependencies subject
1308
1308
1309
1309
Who else might modify `data`?
1310
1310
1311
+
**Warning**: The initialization of global objects is not totally ordered.
1312
+
If you use a global object initialize it with a constant.
1313
+
Note that it is possible to get undefined initialization order even for `const` objects.
1314
+
1315
+
##### Exception
1316
+
1317
+
A global object is often better than a singleton.
1318
+
1311
1319
##### Note
1312
1320
1313
1321
Global constants are useful.
@@ -1322,6 +1330,9 @@ Another solution is to define the data as the state of some object and the opera
1322
1330
**Warning**: Beware of data races: If one thread can access nonlocal data (or data passed by reference) while another thread executes the callee, we can have a data race.
1323
1331
Every pointer or reference to mutable data is a potential data race.
1324
1332
1333
+
Using global pointers or references to access and change non-const, and otherwise non-global,
1334
+
data isn't a better alternative to non-const global variables since that doesn't solve the issues of hidden dependencies or potential race conditions.
1335
+
1325
1336
##### Note
1326
1337
1327
1338
You cannot have a race condition on immutable data.
@@ -1334,7 +1345,8 @@ The rule is "avoid", not "don't use." Of course there will be (rare) exceptions,
1334
1345
1335
1346
##### Enforcement
1336
1347
1337
-
(Simple) Report all non-`const` variables declared at namespace scope.
1348
+
(Simple) Report all non-`const` variables declared at namespace scope and global pointers/references to non-const data.
1349
+
(??? NM: Obviously we can warn about non-`const` statics ... do we want to?)
0 commit comments