15

I come across a lot of dyn_cast in a codebase I am working on.

Is it the same thing as dynamic_cast ? or something different ? I searched a bit but couldn't find much info..

justin
  • 104,054
  • 14
  • 179
  • 226
Cemre Mengü
  • 18,062
  • 27
  • 111
  • 169

2 Answers2

19

dyn_cast is part of the LLVM API (and also is the first, second, third, etc.. hit on google) works just like dynamic_cast, however, one difference is that the class doesn't require a v-table like dynamic_cast. Please see the manual for more info.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166
7

Actually, it is llvm::dyn_cast_or_null which is equivalent to dynamic_cast.

These will produce a null pointer if passed a null pointer, whereas llvm::dyn_cast will bail.

  • Both `llvm::dyn_cast_or_null` and `llvm::dyn_cast` will return a null if the value is not of the required type. The difference between then is whether they accept nulls as arguments. – Emoun Oct 11 '21 at 12:28
  • Good point about how `llvm::dyn_cast_or_null` can accept a null pointer as argument like `dynamic_cast`. However, the point about not requiring the class to have a v-table still holds. Thus, `llvm::dyn_cast_or_null` and `dynamic_cast` are not equivalent. – Hari May 18 '23 at 07:45