updating gh-pages

This commit is contained in:
Andrew Pardoe
2017-09-18 12:02:46 -07:00
parent 3e70a8afde
commit ad098d62da

View File

@@ -4,7 +4,7 @@ layout: default
# <a name="main"></a>C++ Core Guidelines
August 11, 2017
September 18, 2017
Editors:
@@ -598,6 +598,7 @@ Now, there is no explicit mention of the iteration mechanism, and the loop opera
for (auto& x : v) { /* modify x */ }
For more details about for-statements, see [ES.71](#Res-for-range).
Sometimes better still, use a named algorithm:
for_each(v, [](int x) { /* do something with the value of x */ });
@@ -8685,7 +8686,7 @@ What is `Port`? A handy wrapper that encapsulates the resource:
##### Note
Where a resource is "ill-behaved" in that it isn't represented as a class with a destructor, wrap it in a class or use [`finally`](#S-gsl)
Where a resource is "ill-behaved" in that it isn't represented as a class with a destructor, wrap it in a class or use [`finally`](#Re-finally)
**See also**: [RAII](#Rr-raii).
@@ -9107,6 +9108,8 @@ Consider:
X* p1 { new X }; // see also ???
unique_ptr<T> p2 { new X }; // unique ownership; see also ???
shared_ptr<T> p3 { new X }; // shared ownership; see also ???
auto p4 = make_unique<X>(); // unique_ownership, preferable to the explicit use "new"
auto p5 = make_shared<X>(); // shared ownership, preferable to the explicit use "new"
}
This will leak the object used to initialize `p1` (only).
@@ -9523,6 +9526,7 @@ Statement rules:
* [ES.84: Don't (try to) declare a local variable with no name](#Res-noname)
* [ES.85: Make empty statements visible](#Res-empty)
* [ES.86: Avoid modifying loop control variables inside the body of raw for-loops](#Res-loop-counter)
* [ES.87: Don't add redundant `==` or `!=` to conditions](#Res-if)
Arithmetic rules:
@@ -11212,6 +11216,70 @@ The loop control up front should enable correct reasoning about what is happenin
Flag variables that are potentially updated (have a non-`const` use) in both the loop control iteration-expression and the loop body.
### <a name="Res-if"></a>ES.87: Don't add redundant `==` or `!=` to conditions
##### Reason
Doing so avoids verbosity and eliminates some opportunities for mistakes.
Helps make style consistent and conventional.
##### Example
By definition, a condition in an `if`-statement, `while`-statement, or a `for`-statement selects between `true` and `false`.
A numeric value is compared to `0` and a pointer value to `nullptr`.
if (p) { ... } // means "if `p` is not `nullptr`, good
if (p!=0) { ... } // means "if `p` is not `nullptr`, redundant `!=0`; bad: don't use 0 for pointers
if (p!=nullptr) { ... } // means "if `p` is not `nullptr`, redundant `!=nullptr`, not recommended
Often, `if (p)` is read as "if `p` is valid" which is a direct expression of the programmers intent,
whereas `if (p!=nullptr)` would be a long-winded workaround.
##### Example
This rule is especially useful when a declaration is used as a condition
if (auto pc = dynamic_cast<Circle>(ps)) { ... } // execute is ps points to a kind of Circle, good
if (auto pc = dynamic_cast<Circle>(ps); pc!=nullptr) { ... } // not recommended
##### Example
Note that implicit conversions to bool are applied in conditions.
For example:
for (string s; cin>>s; ) v.push_back(s);
This invokes `istream`'s `operator bool()`.
##### Example, bad
It has been noted that
if(strcmp(p1,p2)) { ... } // are the two C-style strings equal? (mistake!)
is a common beginners error.
If you use C-style strings, you must know the `<cstring>` functions well.
Being verbose and writing
if(strcmp(p1,p2)!=0) { ... } // are the two C-style strings equal? (mistake!)
would not save you.
##### Note
The opposite condition is most easily expressed using a negation:
if (!p) { ... } // means "if `p` is`nullptr`, good
if (p==0) { ... } // means "if `p` is `nullptr`, redundant `!=0`; bad: don't use `0` for pointers
if (p==nullptr) { ... } // means "if `p` is `nullptr`, redundant `==nullptr`, not recommended
##### Enforcement
Easy, just check for redundant use of `!=` and `==` in conditions.
## ES.expr: Expressions
Expressions manipulate values.