-
Book Overview & Buying
-
Table Of Contents
C++ Memory Management
By :
Traditionally, C++ has supported four ways to perform those explicit type conversions we call casts – static_cast, dynamic_cast, const_cast, and reinterpret_cast. C++11 has added a fifth one, duration_cast, which is tangentially related to this book but will sometimes show up in examples, particularly when we measure the execution time of a function. Finally, C++20 introduced a sixth case, bit_cast, which is of interest to our work in this book.
The following sections give a brief overview of each C++ cast, along with a few examples of how and when they can be useful.
The best, most efficient tool in our type-casting toolset is static_cast. It’s mostly safe, costs essentially nothing in most cases, and can be used in a constexpr context, which makes it amenable to compile-time maneuvers.
You can use static_cast in situations involving potential risks, such as converting an int to a float...