mirror of
https://github.com/cpp-best-practices/cppbestpractices.git
synced 2025-12-18 19:54:34 +03:00
Update 04-Considering_Safety.md
This commit is contained in:
@@ -94,11 +94,13 @@ Use the C++-style cast (static\_cast<>, dynamic\_cast<> ...) instead of the C-st
|
|||||||
double x = getX();
|
double x = getX();
|
||||||
int i = (int) x;
|
int i = (int) x;
|
||||||
|
|
||||||
// Good Idea
|
// Not a Bad Idea
|
||||||
int i = static_cast<int>(x);
|
int i = static_cast<int>(x);
|
||||||
```
|
```
|
||||||
Additionally the C++ cast style is more visible and has the possibility to search for.
|
Additionally the C++ cast style is more visible and has the possibility to search for.
|
||||||
|
|
||||||
|
But consider refactoring of program logic (for example, additional checking on overflow and underflow) if you need to cast `double` to `int`. Measure three times and cut 0.9999999999981 times.
|
||||||
|
|
||||||
## Additional Resources
|
## Additional Resources
|
||||||
|
|
||||||
[How to Prevent The Next Heartbleed](http://www.dwheeler.com/essays/heartbleed.html) by David Wheeler is a good analysis of the current state of code safety and how to ensure safe code.
|
[How to Prevent The Next Heartbleed](http://www.dwheeler.com/essays/heartbleed.html) by David Wheeler is a good analysis of the current state of code safety and how to ensure safe code.
|
||||||
|
|||||||
Reference in New Issue
Block a user