Is there any reason to prefer static_cast<>
over C style casting? Are they equivalent? Is their any sort of speed difference?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
-
What is the difference between
and in java - How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
In short:
static_cast<>()
gives you a compile time checking ability, C-Style cast doesn't.static_cast<>()
is more readable and can be spotted easily anywhere inside a C++ source code, C_Style cast is'nt.More Explanation:
The static cast performs conversions between compatible types. It is similar to the C-style cast, but is more restrictive. For example, the C-style cast would allow an integer pointer to point to a char.
Since this results in a 4-byte pointer ( a pointer to 4-byte datatype) pointing to 1 byte of allocated memory, writing to this pointer will either cause a run-time error or will overwrite some adjacent memory.
In contrast to the C-style cast, the static cast will allow the compiler to check that the pointer and pointee data types are compatible, which allows the programmer to catch this incorrect pointer assignment during compilation.
You can also check this page on more explanation on C++ casts : Click Here
Since there are many different kinds of casting each with different semantics, static_cast<> allows you to say "I'm doing a legal conversion from one type to another" like from int to double. A plain C-style cast can mean a lot of things. Are you up/down casting? Are you reinterpreting a pointer?
C++ style casts are checked by the compiler. C style casts aren't and can fail at runtime
also, c++ style casts can be searched for easily, whereas it's really hard to search for c style casts
Another big benefit is that the 4 different C++ style casts express the intent of the programmer more clearly.
When writing C++ I'd pretty much always use the C++ ones over the the C style.
static_cast
checks at compile time that conversion is not between obviously incompatible types. Contrary todynamic_cast
, no check for types compatibility is done at run time. Also,static_cast
conversion is not necessarily safe.static_cast
is used to convert from pointer to base class to pointer to derived class, or between native types, such as enum to int or float to int.The user of
static_cast
must make sure that the conversion is safe.The C-style cast does not perform any check, either at compile or at run time.
A great post explaining different casts in C/C++, and what C-style cast really does: https://anteru.net/blog/2007/12/18/200/index.html