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
+30-26Lines changed: 30 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -589,7 +589,7 @@ Better:
589
589
590
590
Now, there is no explicit mention of the iteration mechanism, and the loop operates on a reference to `const` elements so that accidental modification cannot happen. If modification is desired, say so:
591
591
592
-
for (auto& x : v) { /* do to with x */ }
592
+
for (auto& x : v) { /* modify x */ }
593
593
594
594
Sometimes better still, use a named algorithm:
595
595
@@ -4048,17 +4048,17 @@ For example:
4048
4048
##### Note
4049
4049
4050
4050
If the set of direct users of a set of variables cannot be easily determined, the type or usage of that set cannot be (easily) changed/improved.
4051
-
For `public`and `protected` data, that's usually the case.
4051
+
For `public`and `protected` data, that's usually the case.
4052
4052
4053
4053
##### Example
4054
4054
4055
4055
A class can provide two interfaces to its users.
4056
4056
One for derived classes (`protected`) and one for general users (`public`).
4057
-
For example, a derived class might be allowed to skip a run-time check because it has already guarenteed correctness:
4057
+
For example, a derived class might be allowed to skip a run-time check because it has already guaranteed correctness:
4058
4058
4059
4059
class Foo {
4060
4060
public:
4061
-
int bar(int x) { check(x); return do_bar(); }
4061
+
int bar(int x) { check(x); return do_bar(); }
4062
4062
// ...
4063
4063
protected:
4064
4064
int do_bar(int x); // do some operation on the data
@@ -4069,12 +4069,16 @@ For example, a derived class might be allowed to skip a run-time check because i
4069
4069
4070
4070
class Dir : public Foo {
4071
4071
//...
4072
-
int mem(int x, int y) { /* ... do something ... */ rteurn do_bar(x+y); } // OK: derived class can bypass check
4072
+
int mem(int x, int y)
4073
+
{
4074
+
/* ... do something ... */
4075
+
return do_bar(x+y); // OK: derived class can bypass check
4076
+
}
4073
4077
}
4074
4078
4075
4079
void user(Foo& x)
4076
4080
{
4077
-
int r1 = x.bar(1); // OK, will check
4081
+
int r1 = x.bar(1); // OK, will check
4078
4082
int r2 = x.do_bar(2); // error: would bypass check
4079
4083
// ...
4080
4084
}
@@ -6817,14 +6821,14 @@ This kind of "vector" isn't meant to be used as a base class at all.
6817
6821
Style st;
6818
6822
};
6819
6823
6820
-
Now it is up to every defived `Shape` to manipulate the protected data correctly.
6824
+
Now it is up to every derived `Shape` to manipulate the protected data correctly.
6821
6825
This has been popular, but also a major source of maintenance problems.
6822
6826
In a large class hierarchy, the consistent use of protected data is hard to maintain because there can be a lot of code,
6823
6827
spread over a lot of classes.
6824
6828
The set of classes that can touch that data is open: anyone can derive a new class and start manipulating the protected data.
6825
6829
Often, it is not possible to examine the complete set of classes so any change to the representation of the class becomes infeasible.
6826
6830
There is no enforced invariant for the protected data; it is much like a set of global variables.
6827
-
The protected data has de-factor become global to a large body of code.
6831
+
The protected data has de facto become global to a large body of code.
6828
6832
6829
6833
##### Note
6830
6834
@@ -6960,18 +6964,18 @@ or various bases from boost.intrusive (e.g. `list_base_hook` or `intrusive_ref_c
6960
6964
};
6961
6965
6962
6966
class Derive1 : public Interface, virtual protected Utility {
6963
-
// overrride Iterface functions
6967
+
// override Interface functions
6964
6968
// Maybe override Utility virtual functions
6965
6969
// ...
6966
6970
};
6967
6971
6968
6972
class Derive2 : public Interface, virtual protected Utility {
6969
-
// overrride Iterface functions
6973
+
// override Interface functions
6970
6974
// Maybe override Utility virtual functions
6971
6975
// ...
6972
6976
};
6973
6977
6974
-
Factoring out `Utility` makes sense if many derived classes share significent "implementation details."
6978
+
Factoring out `Utility` makes sense if many derived classes share significant "implementation details."
6975
6979
6976
6980
6977
6981
##### Note
@@ -6982,7 +6986,7 @@ and `Utility` is the root of an [implementation hierarchy](Rh-kind).
6982
6986
6983
6987
##### Note
6984
6988
6985
-
Often, lineraization of a hierarchy is a better solution.
6989
+
Often, linearization of a hierarchy is a better solution.
6986
6990
6987
6991
##### Enforcement
6988
6992
@@ -14511,28 +14515,28 @@ Exception specifications make error handling brittle, impose a run-time cost, an
14511
14515
##### Example
14512
14516
14513
14517
int use(int arg)
14514
-
throw(X,Y)
14518
+
throw(X,Y)
14515
14519
{
14516
14520
// ...
14517
14521
auto x = f(arg);
14518
14522
// ...
14519
14523
}
14520
14524
14521
-
if 'f()' throws an exception different from `X` and `Y` the unexpected handler is invoked, which by default terminates.
14525
+
if `f()` throws an exception different from `X` and `Y` the unexpected handler is invoked, which by default terminates.
14522
14526
That's OK, but say that we have checked that this cannot happen and `f` is changed to throw a new exception `Z`,
14523
14527
we now have a crash on our hands unless we change `use()` (and re-test everything).
14524
14528
The snag is that `f()` may be in a library we do not control and the new exception is not anything that `use()` can do
14525
14529
anything about or is in any way interested in.
14526
14530
We can change `use()` to pass `Z` through, but now `use()`'s callers probably needs to be modified.
14527
14531
This quickly becomes unmanageable.
14528
-
Alternatively, we can add a `try`-`catch` to `use()` to map `Z` into an acceptable excption.
14532
+
Alternatively, we can add a `try`-`catch` to `use()` to map `Z` into an acceptable exception.
14529
14533
This too, quickly becomes unmanageable.
14530
14534
Note that changes to the set of exceptions often happens at the lowest level of a system
14531
-
(e.g., because of changes to a network library or some middleware), so changes "bubble up" through long call chains.
14535
+
(e.g., because of changes to a network library or some middleware), so changes "bubble up" through long call chains.
14532
14536
In a large code base, this could mean that nobody could update to a new version of a library until the last user was modified.
14533
-
If `use()` is part of a library, it may not be possible to update it bacause a change could affect unknow clients.
14537
+
If `use()` is part of a library, it may not be possible to update it because a change could affect unknown clients.
14534
14538
14535
-
The policy of letting exceptions propogate until they reach a function that potentially can handle it has proven itself over the years.
14539
+
The policy of letting exceptions propagate until they reach a function that potentially can handle it has proven itself over the years.
14536
14540
14537
14541
##### Note
14538
14542
@@ -14541,7 +14545,7 @@ For example, see [Stroustrup94](#Stroustrup94).
14541
14545
14542
14546
##### Note
14543
14547
14544
-
If no exception may be throwh, use [`noexcept`(#Re-noexcept)]
14548
+
If no exception may be thrown, use [`noexcept`](#Re-noexcept)
14545
14549
14546
14550
##### Enforcement
14547
14551
@@ -17387,7 +17391,7 @@ It is more likely to be stable, well-maintained, and widely available than your
17387
17391
##### Reason
17388
17392
17389
17393
Adding to `std` may change the meaning of otherwise standards conforming code.
17390
-
Additions to `std` may clash with furture versions of the standard.
17394
+
Additions to `std` may clash with future versions of the standard.
17391
17395
17392
17396
##### Example
17393
17397
@@ -17752,7 +17756,7 @@ Iostream rule summary:
17752
17756
##### Reason
17753
17757
17754
17758
Unless you genuinely just deal with individual characters, using character-level input leads to the user code performing potentially error-prone
17755
-
and potentially inefficient compusition ot tokens out of characters.
17759
+
and potentially inefficient composition of tokens out of characters.
17756
17760
17757
17761
##### Example
17758
17762
@@ -17767,7 +17771,7 @@ and potentially inefficient compusition ot tokens out of characters.
17767
17771
17768
17772
##### Reason
17769
17773
17770
-
`iosteam`s are safe, flexible, and extensible.
17774
+
`iostream`s are safe, flexible, and extensible.
17771
17775
17772
17776
##### Example
17773
17777
@@ -17856,7 +17860,7 @@ a `longjmp` ignores destructors, thus invalidating all resource-management strat
17856
17860
17857
17861
##### Enforcement
17858
17862
17859
-
Flag all occurences of `longjmp`and `setjmp`
17863
+
Flag all occurrences of `longjmp`and `setjmp`
17860
17864
17861
17865
17862
17866
@@ -19480,9 +19484,9 @@ Use literal suffixes where clarification is needed
19480
19484
19481
19485
###### Note
19482
19486
19483
-
Literals should not be springled all over the code as ["magic constants'](#Res-magic),
19487
+
Literals should not be sprinkled all over the code as ["magic constants"](#Res-magic),
19484
19488
but it is still a good idea to make them readable where they are defined.
19485
-
It is easy to make a yypo in a long string of integers.
19489
+
It is easy to make a typo in a long string of integers.
19486
19490
19487
19491
###### Enforcement
19488
19492
@@ -20639,7 +20643,7 @@ Alternatively, we will decide that no change is needed and delete the entry.
20639
20643
\[Meyers15]: S. Meyers. Effective Modern C++ (O'Reilly, 2015).
20640
20644
* <a name="Murray93"></a>
20641
20645
\[Murray93]: R. Murray. C++ Strategies and Tactics (Addison-Wesley, 1993).
20642
-
* <a name="Stroustrup94"></a>
20646
+
* <a name="Stroustrup94"></a>
20643
20647
\[Stroustrup94]: B. Stroustrup. The Design and Evolution of C++ (Addison-Wesley, 1994).
20644
20648
* <a name="Stroustrup00"></a>
20645
20649
\[Stroustrup00]: B. Stroustrup. The C++ Programming Language (Special 3rdEdition) (Addison-Wesley, 2000).
0 commit comments