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.
The static_cast operator converts variable j to type float . This allows the compiler to generate a division with an answer of type float . All static_cast operators resolve at compile time and do not remove any const or volatile modifiers.
Static casts are only available in C++. Static casts can be used to convert one type into another, but should not be used for to cast away const-ness or to cast between non-pointer and pointer types.
Static casting is done by the compiler: it treats the result as the target type, no matter what. You do this when you're absolutely sure about the argument being of the target type. Dynamic casting is done at runtime, and thus requires runtime type information.
One advantage which the other two answers didn't mention yet is that static_cast
is much easier to spot. The meaning of parentheses is notoriously overloaded in C++ and it can be difficult to spot evil (or even incorrect) casts. When I see something ending in _cast
though, it's like a mental speed bump: I slow down and carefully check why the type system is being subverted.
I'm assuming that trivial uses of references to avoid pointers won't count.
In that case: a C-style cast can be:
const_cast
static_cast
static_cast
followed by a const_cast
,reinterpret_cast
reinterpret_cast
followed by a const_cast
with the exception that static_cast
's restrictions on inaccessible base classes are lifted.
const_cast
only applies to pointers and references.
reinterpret_cast
only applies to pointers and references. It does include pointer-to-integer conversions and vice versa, but that still involves a pointer type.
That special exception for static_cast
only applies to pointers and references.
So yes, by excluding pointers and references, you've excluded everything that C-style casts support over a static_cast
.
If yes, is
static_cast
used for non-pointer types only in order to maintain coding consistency?
Let's go with an analogy: I wouldn't use a chainsaw to open a bag of chips. I could, but chainsaws are dangerous, so by using one, I'd introduce unnecessary risks. It's very easy to use a chainsaw wrong, and if I do use it wrong, there's no safety mechanism to prevent accidents.
Is
static_cast
used for non-pointer types only in order to maintain coding consistency?
Not only that, but also to help maintain correctness and future compatibility.
It helps maintain correctness, since static_cast
cannot do some casts which a C-style cast can. This helps if you make a mistake about some of the types involved, perhaps handling a pointer while being convinced it's an integer (and down several layers of templates and typedefs, that's not too difficult to imagine). So if you intend a static_cast
, write one and get a compile-time error, you'll be told immediately that the types involved are not what you thought they were. With the C-style cast's "anything goes" attitude, you will not discover the mistake in time.
It also helps with future compatibility. If the types involved in the cast change later in the development, a static_cast
will report an error where a C-style cast would just silently change its behaviour (to something most likely not intended).
is static_cast used for non-pointer types only in order to maintain coding consistency?
No (well - yes, but not only).
It is also easy to find/replace. This is important in case of refactorings, bug fixing, etc.
It is a constraint on the cast type allowed: Consider an example where you have a C-style cast (which works fine) on a variable, then you change the declaration of the casted variable, to something which renders the cast invalid (for example, you change int x;
to void * const x;
).
The C-style cast suddenly performs another function (i.e. const_cast<...>(reinterpret_cast<...>(...)
) with the same code.
If you write the initial code with a static_cast instead of a C-style cast, the compiler will let you know that static_cast in there is not actually performing a static cast.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With