18

Is static_cast<T>(...) something that gets done at compile-time or run-time? I've googled around but I got different answers.

Also, dynamic_cast<T>(...) is obviously runtime - but what about reinterpret_cast<T>(...)?

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • 4
    "Static"... the clue is in the name. – Kerrek SB Aug 26 '13 at 23:31
  • static_cast performs no runtime checks. – dare Aug 26 '13 at 23:33
  • 3
    Judging from the existing answers the bigger question that needs to be decided here is what you mean by *"gets done at compile-time"*. While the conversion to use is chosen at compile-time the actual conversion itself can (and usually will) very well result in conversion code that needs to be executed at runtime. – Christian Rau Aug 27 '13 at 11:18

2 Answers2

14

Depends on what you are casting to what else. E.g. static_cast<std::string>("Hello") ends up calling std::string constructor.

Off the top of my head, I can't think of any case where reinterpret_cast would need to generate actual machine instructions. It's just telling the compiler: take this bit pattern, and believe it to be a value this type.

Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
  • Oh, interesting. What are the rules for this? Does this happen when I cast a `int` to a `float`? And when I cast a `float` to an `int`? – Vittorio Romeo Aug 26 '13 at 23:39
  • Both would likely need to generate some machine instructions, unless you are casting a compile-time constant (that is, if you write `static_cast(42)`, the compiler would usually be smart enough to just replace it with `42f` at compile time). – Igor Tandetnik Aug 26 '13 at 23:42
  • Would that be optimized with `-O3`? Or should I use a `reinterpret_cast` when I'm sure of what I'm doing for performance-intense numeric casts? – Vittorio Romeo Aug 26 '13 at 23:45
  • 7
    You should **not** use `reinterpret_cast` unless absolutely necessary, and you understand perfectly what you are doing. `reinterpret_cast(42)` does **not** produce 42f: it produces some floating point value that just happens to have the same bit representation as the integer 42. Try it, it should prove educational. – Igor Tandetnik Aug 26 '13 at 23:48
  • 4
    @VittorioRomeo Even if `int` and `float` are the same size, they don't share the same binary representation. So a `reinterpret_cast` to convert between the two is almost assuredly not what you want. In fact, you should probably lay off `reinterpret_cast` altogether until you know exactly what you're doing. – Praetorian Aug 26 '13 at 23:49
5

Compile time. In fact, the compiler doesn't even insert runtime code to check that the result is correct. The compiler does check that the conversion is statically possible, of course. Example: casting from a subclass to a superclass. If the conversion requires invoking a builtin or a casting function, they will be executed at runtime, of course, but there will be no type checking.

Mario Rossi
  • 7,651
  • 27
  • 37
  • 4
    Even casting from subclass to superclass may result in run-time code having to be generated, e.g. when said superclass is in fact a virtual base class. – Igor Tandetnik Aug 26 '13 at 23:38
  • @IgorTandetnik Right. I meant type-checking code. Tried to correct the ambiguity (or plain error! :-) ). – Mario Rossi Aug 26 '13 at 23:46