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
+22-20Lines changed: 22 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -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.
@@ -19201,6 +19200,7 @@ to files that includes it or in scenarios where the different search algorithm i
19201
19200
#include "helpers.h" // A project specific file, use "" form
19202
19201
19203
19202
##### Note
19203
+
19204
19204
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.
19205
19205
19206
19206
Library creators should put their headers in a folder and have clients include those files using the relative path `#include <some_library/common.h>`
@@ -20282,9 +20282,11 @@ and errors (when we didn't deal correctly with semi-constructed objects consiste
20282
20282
// main problem: constructor does not fully construct
20283
20283
Picture(int x, int y)
20284
20284
{
20285
-
mx = x; // also bad: assignment in constructor body rather than in member initializer
20285
+
mx = x; // also bad: assignment in constructor body
20286
+
// rather than in member initializer
20286
20287
my = y;
20287
-
data = nullptr; // also bad: constant initialization in constructor rather than in member initializer
20288
+
data = nullptr; // also bad: constant initialization in constructor
20289
+
// rather than in member initializer
20288
20290
}
20289
20291
20290
20292
~Picture()
@@ -20466,7 +20468,7 @@ Reference sections:
20466
20468
Libraries used have to have been approved for mission critical applications.
20467
20469
Any similarities to this set of guidelines are unsurprising because Bjarne Stroustrup was an author of JSF++.
20468
20470
Recommended, but note its very specific focus.
20469
-
* [_MISRA C++ 2008: Guidelines for the use of the C++ language in critical systems_] (https://www.misra.org.uk/Buyonline/tabid/58/Default.aspx).
20471
+
* [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