Skip to content

Commit 153da17

Browse files
authored
Merge I.2 and R.6 seem to address the same issue, (#1553)
thus I propose removing R.6 and moving some of its contents into I.2.
1 parent 9a4db6c commit 153da17

File tree

1 file changed

+14
-22
lines changed

1 file changed

+14
-22
lines changed

CppCoreGuidelines.md

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,14 @@ Non-`const` global variables hide dependencies and make the dependencies subject
13081308

13091309
Who else might modify `data`?
13101310

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+
13111319
##### Note
13121320

13131321
Global constants are useful.
@@ -1322,6 +1330,9 @@ Another solution is to define the data as the state of some object and the opera
13221330
**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.
13231331
Every pointer or reference to mutable data is a potential data race.
13241332

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+
13251336
##### Note
13261337

13271338
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,
13341345

13351346
##### Enforcement
13361347

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?)
13381350

13391351
### <a name="Ri-singleton"></a>I.3: Avoid singletons
13401352

@@ -8992,7 +9004,6 @@ Here, we ignore such cases.
89929004
* [R.3: A raw pointer (a `T*`) is non-owning](#Rr-ptr)
89939005
* [R.4: A raw reference (a `T&`) is non-owning](#Rr-ref)
89949006
* [R.5: Prefer scoped objects, don't heap-allocate unnecessarily](#Rr-scoped)
8995-
* [R.6: Avoid non-`const` global variables](#Rr-global)
89969007

89979008
* Allocation and deallocation rule summary:
89989009

@@ -9284,26 +9295,7 @@ Instead, use a local variable:
92849295

92859296
### <a name="Rr-global"></a>R.6: Avoid non-`const` global variables
92869297

9287-
##### Reason
9288-
9289-
Global variables can be accessed from everywhere so they can introduce surprising dependencies between apparently unrelated objects.
9290-
They are a notable source of errors.
9291-
9292-
**Warning**: The initialization of global objects is not totally ordered.
9293-
If you use a global object initialize it with a constant.
9294-
Note that it is possible to get undefined initialization order even for `const` objects.
9295-
9296-
##### Exception
9297-
9298-
A global object is often better than a singleton.
9299-
9300-
##### Exception
9301-
9302-
An immutable (`const`) global does not introduce the problems we try to avoid by banning global objects.
9303-
9304-
##### Enforcement
9305-
9306-
(??? NM: Obviously we can warn about non-`const` statics ... do we want to?)
9298+
See [I.2](#Ri-global)
93079299

93089300
## <a name="SS-alloc"></a>R.alloc: Allocation and deallocation
93099301

0 commit comments

Comments
 (0)