<div dir="ltr">If you don't expect the cast to fail because you have special knowledge about its derived type, then you should just use std::static_pointer_cast.  If you do think it might fail at runtime and the only way to know is to try the dynamic_cast, then you have to use the llvm casting infrastructure because it is basically LLVM's replacement for dynamic_cast and RTTI.  So it's up to you to add support for classof() etc to the base and derived.  Then you can write:<div><br></div><div>if (llvm::isa<Derived>(x.get())) {</div><div>  auto p = std::static_pointer_cast<Derived>(x);</div><div>}</div><div><br></div><div>I don't think we have an llvm::shared_dyn_cast<>, but it might be reasonable to add one that encapsulated the above if-statement so that you could write:</div><div><br></div><div>if (auto p = llvm::shared_dyn_cast<Derived>(x)) {</div><div>  ...</div><div>}</div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Jul 31, 2017 at 1:55 PM Vedant Kumar <<a href="mailto:vsk@apple.com">vsk@apple.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><blockquote type="cite"><div>On Jul 31, 2017, at 5:43 AM, Victor Campos via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="m_-5384357166358300734Apple-interchange-newline"><div><div dir="ltr"><div><div>Hi,<br><br></div>I would like to use std::shared_ptr in my pass. However I'm facing a problem wrt RTTI. If I have a code like:<br><br></div><div><span style="font-family:monospace,monospace">std::shared_ptr<BaseClass> x(new DerivedClass());<br>...<br></span></div><div><span style="font-family:monospace,monospace">std::shared_ptr<DerivedClass> p = std::dynamic_pointer_cast<DerivedClass>(x);</span><br><br></div><div>It does not compile since the default RTTI infrastructure is not used by LLVM. Also, it's not clear to me if the 'classof' approach works in this case (I did try it with no success).<br><br></div><div>Is it possible to have a dynamic_cast using std smart pointers in LLVM?</div></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word"><div><span>I don't know of an off-the-shelf way to do this but you could look to r300098 for inspiration (it introduced unique_dyn_cast).</span></div><div><span><br></span></div><div><span>vedant<br></span></div></div><div style="word-wrap:break-word"><div><span><br></span><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>Thanks.<br></div></div>
_______________________________________________<br>LLVM Developers mailing list<br><a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br></div></blockquote></div></div></blockquote></div>