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
+23-21
Original file line number
Diff line number
Diff line change
@@ -4492,7 +4492,7 @@ Destructor rules:
4492
4492
* [C.30: Define a destructor if a class needs an explicit action at object destruction](#Rc-dtor)
4493
4493
* [C.31: All resources acquired by a class must be released by the class's destructor](#Rc-dtor-release)
4494
4494
* [C.32: If a class has a raw pointer (`T*`) or reference (`T&`), consider whether it might be owning](#Rc-dtor-ptr)
4495
-
* [C.33: If a class has an owning pointer member, define or `=delete` a destructor](#Rc-dtor-ptr2)
4495
+
* [C.33: If a class has an owning pointer member, define a destructor](#Rc-dtor-ptr2)
4496
4496
* [C.35: A base class destructor should be either public and virtual, or protected and non-virtual](#Rc-dtor-virtual)
4497
4497
* [C.36: A destructor may not fail](#Rc-dtor-fail)
4498
4498
* [C.37: Make destructors `noexcept`](#Rc-dtor-noexcept)
@@ -12698,7 +12698,7 @@ consider `gsl::finally()` as a cleaner and more reliable alternative to `goto ex
12698
12698
12699
12699
switch(x){
12700
12700
case 1 :
12701
-
while(/* some condition */){
12701
+
while(/* some condition */){
12702
12702
//...
12703
12703
break;
12704
12704
} //Oops! break switch or break while intended?
@@ -12715,8 +12715,8 @@ Often, a loop that requires a `break` is a good candidate for a function (algori
12715
12715
void use1(){
12716
12716
std::vector<T> vec = {/* initialized with some values */};
12717
12717
T value;
12718
-
for(const T item : vec){
12719
-
if(/* some condition*/){
12718
+
for(const T item : vec){
12719
+
if(/* some condition*/){
12720
12720
value = item;
12721
12721
break;
12722
12722
}
@@ -12725,30 +12725,30 @@ Often, a loop that requires a `break` is a good candidate for a function (algori
12725
12725
}
12726
12726
12727
12727
//BETTER: create a function and return inside loop
12728
-
T search(const std::vector<T> &vec){
12729
-
for(const T &item : vec){
12730
-
if(/* some condition*/) return item;
12728
+
T search(const std::vector<T> &vec){
12729
+
for(const T &item : vec){
12730
+
if(/* some condition*/) return item;
12731
12731
}
12732
12732
return T(); //default value
12733
12733
}
12734
12734
12735
-
void use2(){
12735
+
void use2(){
12736
12736
std::vector<T> vec = {/* initialized with some values */};
12737
12737
T value = search(vec);
12738
12738
/* then do something with value */
12739
12739
}
12740
12740
12741
12741
Often, a loop that uses `continue` can equivalently and as clearly be expressed by an `if`-statement.
12742
12742
12743
-
for(int item : vec){ //BAD
12744
-
if(item%2 == 0) continue;
12745
-
if(item == 5) continue;
12746
-
if(item > 10) continue;
12743
+
for(int item : vec){ //BAD
12744
+
if(item%2 == 0) continue;
12745
+
if(item == 5) continue;
12746
+
if(item > 10) continue;
12747
12747
/* do something with item */
12748
12748
}
12749
12749
12750
-
for(int item : vec){ //GOOD
12751
-
if(item%2 != 0 && item != 5 && item <= 10){
12750
+
for(int item : vec){ //GOOD
12751
+
if(item%2 != 0 && item != 5 && item <= 10){
12752
12752
/* do something with item */
12753
12753
}
12754
12754
}
@@ -14972,21 +14972,20 @@ There is no explicit locking and both correct (value) return and error (exceptio
14972
14972
return value;
14973
14973
}
14974
14974
14975
-
14976
14975
void async_example()
14977
14976
{
14978
14977
try
14979
14978
{
14980
-
auto v1 = std::async(std::launch::async, read_value, "v1.txt");
14979
+
auto v1 = std::async(std::launch::async, read_value, "v1.txt");
14981
14980
auto v2 = std::async(std::launch::async, read_value, "v2.txt");
14982
14981
std::cout << v1.get() + v2.get() << '\n';
14983
14982
}
14984
-
catch (std::ios_base::failure & fail)
14983
+
catch (std::ios_base::failure & fail)
14985
14984
{
14986
14985
// handle exception here
14987
14986
}
14988
14987
}
14989
-
14988
+
14990
14989
##### Note
14991
14990
14992
14991
Unfortunately, `async()` is not perfect.
@@ -19200,6 +19199,7 @@ Nevertheless, the guidance is to use the quoted form for including files that ex
19200
19199
#include "foo_utils/utils.h" // A file locally relative to foo.cpp, use "" form
19201
19200
19202
19201
##### Note
19202
+
19203
19203
Failing to follow this results in difficult to diagnose errors due to picking up the wrong file by incorrectly specifying the scope when it is included. For example, in a typical case where the `#include ""` search algorithm may search for a file existing at a local relative path first, then using this form to refer to a file that is not locally relative could mean that if a file ever comes into existence at the local relative path (e.g. the including file is moved to a new location), it will now be found ahead of the previous include file and the set of includes will have been changed in an unexpected way.
19204
19204
19205
19205
Library creators should put their headers in a folder and have clients include those files using the relative path `#include <some_library/common.h>`
@@ -20281,9 +20281,11 @@ and errors (when we didn't deal correctly with semi-constructed objects consiste
20281
20281
// main problem: constructor does not fully construct
20282
20282
Picture(int x, int y)
20283
20283
{
20284
-
mx = x; // also bad: assignment in constructor body rather than in member initializer
20284
+
mx = x; // also bad: assignment in constructor body
20285
+
// rather than in member initializer
20285
20286
my = y;
20286
-
data = nullptr; // also bad: constant initialization in constructor rather than in member initializer
20287
+
data = nullptr; // also bad: constant initialization in constructor
20288
+
// rather than in member initializer
20287
20289
}
20288
20290
20289
20291
~Picture()
@@ -20465,7 +20467,7 @@ Reference sections:
20465
20467
Libraries used have to have been approved for mission critical applications.
20466
20468
Any similarities to this set of guidelines are unsurprising because Bjarne Stroustrup was an author of JSF++.
20467
20469
Recommended, but note its very specific focus.
20468
-
* [_MISRA C++ 2008: Guidelines for the use of the C++ language in critical systems_] (https://www.misra.org.uk/Buyonline/tabid/58/Default.aspx).
20470
+
* [MISRA C++ 2008: Guidelines for the use of the C++ language in critical systems](https://www.misra.org.uk/Buyonline/tabid/58/Default.aspx).
0 commit comments