Is there any reason to prefer static_cast<>
over C style casting? Are they equivalent? Is there any sort of speed difference?
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.
dynamic_cast
s. –
Constance (int)something
can't fail - either you get cast to int or compiler error. –
Oswald reinterpret_cast
s" and analyze them. –
Eddie 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.- Intentions are conveyed much better using C++ casts.
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.
char c = 10; // 1 byte
int *p = (int*)&c; // 4 bytes
Since this results in a pointer to a 4-byte type, pointing to 1 byte of allocated memory, writing to this pointer will either cause a run-time error or will overwrite some adjacent memory.
*p = 5; // run-time error: stack corruption
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.
int *q = static_cast<int*>(&c); // compile-time error
You can also check this page on more explanation on C++ casts : Click Here
int q = c;
. The type of the initialized variable dominates and the initializer is converted to the that type. Hence, the c
will be promoted to int
and then this result will be used to initialize the q
. –
Sipes See A comparison of the C++ casting operators.
However, using the same syntax for a variety of different casting operations can make the intent of the programmer unclear.
Furthermore, it can be difficult to find a specific type of cast in a large codebase.
the generality of the C-style cast can be overkill for situations where all that is needed is a simple conversion. The ability to select between several different casting operators of differing degrees of power can prevent programmers from inadvertently casting to an incorrect type.
struct A {};
struct B : A {};
struct C {};
int main()
{
A* a = new A;
int i = 10;
a = (A*) (&i); // NO ERROR! FAIL!
//a = static_cast<A*>(&i); ERROR! SMART!
A* b = new B;
B* b2 = static_cast<B*>(b); // NO ERROR! SMART!
C* c = (C*)(b); // NO ERROR! FAIL!
//C* c = static_cast<C*>(b); ERROR! SMART!
}
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
C-Style casting, using the (type)variable syntax. The worst ever invented. This tries to do the following casts, in this order: (see also C++ Standard, 5.4 expr.cast paragraph 5)
- const_cast
- static_cast
- static_cast followed by const_cast
- reinterpret_cast
- reinterpret_castfollowed by const_cast
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?
static_cast
checks at compile time that conversion is not between obviously incompatible types. Contrary to dynamic_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.
© 2022 - 2024 — McMap. All rights reserved.