Is dynamic_cast a built-in of C++? I looked through GCC headers, /usr/include/c++/4.4.7 in my installment and could not find its implementation.
Asked
Active
Viewed 469 times
0
-
2I believe it's a keyword. – Jan 14 '14 at 20:07
-
Yes, it's a keyword. Any good book or other reference on C++ should tell you what is and isn't part of the core language. – Jan 14 '14 at 20:09
-
That actually doesn't prevent it from being implemented using (admittedly non-portable) C++ source. At least the dynamic part, that is, the static part (are the surce and target types even related?) has to be done by the compiler. – MSalters Jan 15 '14 at 10:16
2 Answers
4
dynamic_cast
is built in to the language. It does require an implementation, but there is no standardised location for the implementation.
In the gcc tool-chain, you can find some helper functions for the implementation in libsupc++. You might also be interested in reading the ABI documentation for your platform -- for example, the x64 C++ ABI indicates the required storage layout and provides some sample code.

rici
- 234,347
- 28
- 237
- 341
-
That doesn't contain the implementation of dynamic_cast, it contains the implementation of the helper functions used by GCC's implementation of dynamic_cast, which is not the same thing. Those helper functions may well be what the OP is looking for, though. – Jan 14 '14 at 20:15
-
@hvd: tried to fix the language, and added a pointer to x64 abi (I hope still current) – rici Jan 14 '14 at 20:18
-
Looks good to me. Re-reading my comment, it may come across as a smart-ass remark, but to clarify, there are definitely bits of `dynamic_cast` that you can only find in the compiler sources, not any run-time library, a simple example being compile-time errors for invalid cast attempts ("error: cannot dynamic_cast 's' (of type 'struct S*') to type 'struct T*' (source type is not polymorphic)") – Jan 14 '14 at 20:20
-
@hvd: sure, I agree. Furthermore, the compiler is free to optimize dynamic_cast based on any information it may be able to deduce so an individual cast site may not need any function call. So, as usual, it's Not Quite That Simple :) . – rici Jan 14 '14 at 20:26