[Bug libstdc++/81078] dynamic_cast to virtual base produces the wrong answer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81078 --- Comment #3 from Arthur O'Dwyer --- Yes, this is a libstdc++ issue. I'm not 100% sure that "the RTTI [generated by GCC] is correct," because I don't know how to use GCC with libc++; but yeah, there's definitely at least some problem with libstdc++ here. Observe that Clang-with-libstdc++ fails, Clang-with-libc++ succeeds: https://godbolt.org/z/PMq6rW9cE
[Bug libstdc++/81078] dynamic_cast to virtual base produces the wrong answer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81078 Andrew Pinski changed: What|Removed |Added Last reconfirmed||2021-08-18 Status|UNCONFIRMED |NEW Ever confirmed|0 |1 --- Comment #2 from Andrew Pinski --- (In reply to Arthur O'Dwyer from comment #0) you need to use libc++ to get the answer. This means the RTTI is correct and it is libstdc++ issue?
[Bug libstdc++/81078] dynamic_cast to virtual base produces the wrong answer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81078 --- Comment #1 from Arthur O'Dwyer --- I've found that the misbehavior actually depends on the *ordering* of bases in the class graph, not just the shape of the graph. // https://wandbox.org/permlink/DaKQxTc5Ldqj9FlY #include struct Sideboard { virtual ~Sideboard() {} }; struct Shared { virtual ~Shared() {} }; struct Public : public virtual Shared {}; struct Protected : protected virtual Shared {}; template struct Main : Ts... {}; template void test() { Main m; Sideboard *sb = Shared *sh = if (dynamic_cast(sb) != sh) { puts(__PRETTY_FUNCTION__); } } int main() { test (); test (); test (); test (); test (); test (); } OUTPUT is: void test() [with Ts = {Public, Protected, Sideboard}] That is, the failure case seems to be when (and only when): - the base being casted "from" is laid out after any base descended from "to", AND - the last base (in lexical order) descended from "to" is not publicly descended from "to".