On Thu, Oct 11, 2012 at 7:10 PM, Eli Friedman <span dir="ltr"><<a href="mailto:eli.friedman@gmail.com" target="_blank">eli.friedman@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">On Thu, Oct 11, 2012 at 4:31 PM, Sean Silva <<a href="mailto:silvas@purdue.edu">silvas@purdue.edu</a>> wrote:<br>
> Author: silvas<br>
> Date: Thu Oct 11 18:31:18 2012<br>
> New Revision: 165769<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=165769&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=165769&view=rev</a><br>
> Log:<br>
> Add missing classof().<br>
><br>
> Somewhat troublingly, without this implemented, the check inside<br>
> isa_impl<> would silently use the parent's `classof()` when determining<br>
> whether it was okay to downcast from the parent to the child!<br>
><br>
> Bug analysis:<br>
><br>
> A build failure after removing the parent's `classof()` initially<br>
> alerted me to the bug, after which a little bit of thinking and reading<br>
> of the code identified the root cause.<br>
><br>
> The compiler could be made to prevent this bug from happening if there<br>
> were a way to ensure that in the code<br>
><br>
>     template <typename To, typename From, typename Enabler = void><br>
>     struct isa_impl {<br>
>       static inline bool doit(const From &Val) {<br>
>         return To::classof(&Val);<br>
>       }<br>
>     };<br>
><br>
> that `To::classof` is actually inside the class `To`, and not in a base<br>
> class. I am not aware of a way to check this in C++. If there is a means<br>
> to perform that check, please bring it up on the list and this will be<br>
> fixed.<br>
<br>
</div>Stupid attempt:<br>
<br>
struct A { void f(); };<br>
struct B : A {<br>
#ifdef B_HAS_F<br>
  void f();<br>
#endif<br>
};<br>
decltype(&B::f) x;<br>
extern void (B::*x)(); // error unless B_HAS_F<br>
<br>
Unfortunately, not a complete solution, because you don't know the<br>
precise signature of the classof you're looking for.</blockquote><div><br></div><div>I was pondering that (and you can solve the immediate problem pretty straightforwardly), but I don't think it can be made to work if B::f is overloaded.</div>
</div>