mirror of
https://github.com/cpp-best-practices/cppbestpractices.git
synced 2025-12-17 03:04:36 +03:00
Add note on assert
This commit is contained in:
@@ -31,6 +31,22 @@ See http://mortoray.com/2015/06/15/get-rid-of-those-boolean-function-parameters/
|
|||||||
|
|
||||||
Know and understand the existing C++ standard algorithms and put them to use. See [C++ Seasoning](https://www.youtube.com/watch?v=qH6sSOr-yk8) for more details.
|
Know and understand the existing C++ standard algorithms and put them to use. See [C++ Seasoning](https://www.youtube.com/watch?v=qH6sSOr-yk8) for more details.
|
||||||
|
|
||||||
|
## Never Use `assert` With Side Effects
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// Bad Idea
|
||||||
|
assert(set_value(something));
|
||||||
|
|
||||||
|
// Better Idea
|
||||||
|
[[maybe_unused]] const auto success = set_value(something);
|
||||||
|
assert(success);
|
||||||
|
```
|
||||||
|
|
||||||
|
The `assert()` will be removed in release builds which will prevent the `set_value` call from every happening.
|
||||||
|
|
||||||
|
So while the second version is uglier, the first version is simply not correct.
|
||||||
|
|
||||||
|
|
||||||
## Properly Utilize 'override' and 'final'
|
## Properly Utilize 'override' and 'final'
|
||||||
|
|
||||||
These keywords make it clear to other developers how virtual functions are being utilized, can catch potential errors if the signature of a virtual function changes, and can possibly [hint to the compiler](http://stackoverflow.com/questions/7538820/how-does-the-compiler-benefit-from-cs-new-final-keyword) of optimizations that can be performed.
|
These keywords make it clear to other developers how virtual functions are being utilized, can catch potential errors if the signature of a virtual function changes, and can possibly [hint to the compiler](http://stackoverflow.com/questions/7538820/how-does-the-compiler-benefit-from-cs-new-final-keyword) of optimizations that can be performed.
|
||||||
|
|||||||
Reference in New Issue
Block a user