When any g++ optimization (i.e., other than -O0) is specified, and the lvalue operand of the C++ 'typeid' operator is a C++ reference to an object of polymorphic class type, then the typeid operator incorrectly evaluates to the reference's static type. It should instead evaluate to the referenced object's "most derived" class type. When optimization is not used, the 'typeid' operator correctly evaluates to the referenced object's "most derived" type. Release: 3.2.2 Environment: Red Hat 8.0 Linux, i686 (Pentium-III Celeron) PC How-To-Repeat: 1) Unpack the attached tar.gz file, which contains the C++ source file 'main.cpp'. 2) Example of correct 'typeid' evaluation (NO g++ optimization): [bash]$ g++ -Wall -Werror main.cpp [bash]$ ./a.out A : 1A B : 1B aref : 1B // <-- correct *aptr : 1B 3) Example of incorrect 'typeid' evaluation (with g++ optimization): [bash]$ g++ -Wall -Werror -O main.cpp [bash]$ ./a.out A : 1A B : 1B aref : 1A // <-- wrong *aptr : 1B
State-Changed-From-To: open->analyzed State-Changed-Why: Confirmed. Here's a smaller testcase: --------------------- #include <cstdlib> #include <typeinfo> struct A { virtual ~A() { } }; struct B : public A { }; int main() { B bobj; A& aref = bobj; if (typeid(aref) != typeid(B)) abort(); return 0; } -------------------------------- It executes just fine with 3.0, 3.3 and 3.4, but aborts with 3.2 when given -O2. So it's a regression of 3.2 only. W.
State-Changed-From-To: analyzed->closed State-Changed-Why: Fixed for the next release (3.3).