http://gcc.gnu.org/bugzilla/show_bug.cgi?id=61020
Bug ID: 61020
Summary: [4.9/4.10 Regression] typeid(typeid(X)) produces 'ud2'
Product: gcc
Version: unknown
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: ppluzhnikov at google dot com
The test case:
#include <typeinfo>
struct Base {
virtual ~Base() { }
};
struct Derived : public Base {
};
int compare(const Base& base)
{
return typeid(base) == typeid(typeid(Derived));
}
int main()
{
Base base;
Derived derived;
if (compare(base)) return 1;
if (compare(derived)) return 2;
return 0;
}
Using trunk @ r209848
g++ -g t.cc && ./a.out && echo OK
OK
g++ -g t.cc -O2 && ./a.out
Segmentation fault (core dumped)
(gdb) disas main
Dump of assembler code for function main():
0x00000000004004c0 <+0>: mov 0x8,%rax
0x00000000004004c8 <+8>: ud2
End of assembler dump.
It appears that GCC believes the test to invoke undefined behavior.
However, I don't see anything in the standard to support this.
P.S. Same result in C++98 and C++11
P.P.S. In the original code, the double application of typeid() was a bug.