Are constant and reinterpreted casts done at compile time?

I have read what static_cast

happens at compile time and dynamic_cast

what happens at run time is thus slower than static_cast

. A dynamic_cast

can either return a null ptr (when used with a pointer) or otherwise throw an arbitrary cast exception. My question is what to do with reinterpret_cast

and const_cast

at compile time or runtime? I would have thought that the interpretation of the cast happens at runtime, since it behaves like dynamic_cast

indicating that the cast was successful. I'm right? How about const_cast is compile time?

+3


source to share


3 answers


The dynamic cast is the only one that needs to be "computed" at runtime. All other casts are evaluated at compile time.



  • The machine code for static_cast

    is a fixed function based on the type you produce FROM and TO.
  • The machine code for const_cast

    really does nothing more than pass a const value as no-const or vice versa. So it can be solved at compile time
  • For reinterpret_cast

    machine code can also be resolved at compile time. Once it is nothing but "look at the pointer pointing to type A through the eyes of someone looking for type B".
  • dynamic_cast

    you need to enable virtual tables and set up the correct virtual method addresses based on the FROM and TO types. That's why it's harder!
+11


source


There is a very detailed specification here that answers your question:

http://en.cppreference.com/w/cpp/language/reinterpret_cast



Effectively, reinterpret_cast <> leaves no trace at runtime.

const_cast is also compile time, no code should be generated.

+2


source


  • const_cast

    • used to add or remove a constant is entirely compile time - it doesn't affect the actual code generated to run at runtime - only if the compiler stops with an error when it sees code trying to write a value const

      .

    • used to add or remove volatility - occurs at compile time, but then the compiler can usually generate more and / or slower code when adding volatile

      or less / faster code when removing volatiles; these code changes affect execution performance (and correctness if volatility is required)

  • reinterpret_cast

    changes the compiler's perspective to data and may result in different code execution at runtime; while the startup cost is not tied to the sheet itself, a cast-to type may require more or less code (and processing cycles) in the surrounding context of use than the required cast type (if it even supports these usage semantics)

  • static_cast

    also selects the appropriate code to insert at compile time as well reinterpret_cast

    , but the data itself may undergo additional transformation ( static_cast<T>(x)

    equivalent T temp(x);

    ) ... the transformation can explicitly add code and runtime overhead

  • dynamic_cast

    : the compiler considers its knowledge of real types.

    • If the context of the code in which it is being executed dynamic_cast

      only knows about the cast-from object via a pointer or reference from some other code and cannot determine the real runtime type, it is obligated to use the runtime type information (RTTI) to find out if execution type to cast-to type.

    • If the actual type of discard is known at compile time, the compiler can replace the equivalent static_cast

      between two pointers or reference types. Using his knowledge of the actual types, he can determine whether the pointer / reference needs to be adjusted (like adding or subtracting a certain number of bytes corresponding to the offset of one of the classes in another) or ultimately effectively reinterpret_cast

      the same address.

+2


source







All Articles