0

I have seen some good posts explaining in depth about various c++ cast. One of them is

When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?

What I'm curious about is when static cast is unavoidable? What are the things impossible to do without using static_cast operator?

Community
  • 1
  • 1
NightFurry
  • 358
  • 1
  • 17
  • Dynamic_cast and reinterpret_cast are working on pointers/references, so if you want to cast a nonpointer/reference types (e.g: float to double) you can only use static cast. – Melkon Aug 27 '15 at 09:55
  • CRTPs won't work without `static_cast<>` for example. – πάντα ῥεῖ Aug 27 '15 at 09:58
  • You can use reinterpret_cast for CRTP... even if it's a bad idea, the question was what is impossible to do without static_cast. – Melkon Aug 27 '15 at 09:59
  • 3
    You can do anything with C-old style cast; what you can't do is knowing at compile-time wether you are doing something bad – Paolo M Aug 27 '15 at 10:16
  • 1
    C style cast is a superset of static cast. Any cast which compile with static cast will compile with C style cast. Which means static cast cannot be called "unavoidable" – UmNyobe Aug 27 '15 at 10:32

3 Answers3

1

The main reason to use a static_cast<> over a dynamic_cast<> is performance. With the dynamic_cast<>, you get code that actually checks the dynamic type of the object, adjusting the pointer as appropriate; a static_cast<> always compiles to a single addition instruction at most. And since the dynamic type check may get expensive, static_cast<> can be orders of magnitude faster than the corresponding dynamic_cast<>.

Apart from this performance consideration, static_cast<> is not strictly needed, you can do anything it does with dynamic_cast<> or C-style casts.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
  • My last three words were "or C-style casts". There is no conversion that you cannot do via a C-style cast, which is precisely why people avoid them. But they are available, and their scope encompasses the entire realm of `static_cast<>`. So, `static_cast<>` is not strictly necessary. Neither is `const_cast<>`, nor `reinterpret_cast<>`, both for the same reason. Only the `dynamic_cast<>` does something that a C-style cast can't do. – cmaster - reinstate monica Aug 27 '15 at 10:45
0

One of my favorite examples is casting of enums.

namespace example
{
    enum rate
    {
    GOOD,
    BAD,
    UNDEFINED
    };
    enum checkstate
    {
    CHECKED,
    UNCHECKED,
    UNDEFINED
    };
}

if you have a function accepting example::checkstate and you want to pass example::rate, then the best way is to use static_cast. This is also the case of casting integer values to enum values.

Sunny
  • 517
  • 5
  • 17
0

As has already been pointed out, there's nothing a static_cast can do that a C-style cast or function-style cast can't do. The main advantage of a static_cast is readability; they really jump out on the screen so you can see that a cast is taking place.

Carlton
  • 4,217
  • 2
  • 24
  • 40