简体   繁体   中英

Is static_cast<T>(...) compile-time or run-time?

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>(...) ?

Depends on what you are casting to what else. Eg 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM