The following const-incorrect program compiles without error on gcc 4.1.0, and 4.3.0 20070323: class Foo {}; class Bar : public Foo {}; static const Foo *foo = 0; static Bar *bar = static_cast<const Bar*>(foo); // Error, casts away const void func(const Foo *foo) { Bar *bar = static_cast<const Bar*>(foo); // Error, casts away const } gcc 2.95.3, 3.4.5, and 4.0.2 report errors on the commented lines above, so it looks like a compiler regression in 4.1.0. I looked hard through existing PRs, and couldn't find this reported elsewhere (the closest is PR 20475, but it's concerned with 3.4.4 and the const-ness, or otherwise, of string literals, so not the same). I'd be a little surprised if I'm the first to see it, but it's possible...
Confirmed. The regression was introduced between GCC 4.0.2 and 4.0.3. Mark, it looks like the following patch of yours is responsible for the regression: 2006-01-21 Mark Mitchell <mark@codesourcery.com> PR c++/25895 * class.c (build_base_path): Generate a NOP_EXPR instead of a COMPONENT_REF if the base and derived classes are at the same address.
Subject: Bug 31449 Author: mmitchel Date: Mon Apr 9 20:45:53 2007 New Revision: 123679 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=123679 Log: PR c++/31449 * class.c (build_base_path): Ensure that the converted pointer has the same cv-qualification as the input. PR c++/31449 * g++.dg/init/const5.C: New test. Added: trunk/gcc/testsuite/g++.dg/init/const5.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/class.c trunk/gcc/testsuite/ChangeLog
Fixed in 4.3.0.
Subject: Bug 31449 Author: mmitchel Date: Tue Apr 10 04:01:08 2007 New Revision: 123690 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=123690 Log: PR c++/31449 * class.c (build_base_path): Ensure that the converted pointer has the same cv-qualification as the input. PR c++/31449 * g++.dg/init/const5.C: New test. Added: branches/gcc-4_2-branch/gcc/testsuite/g++.dg/init/const5.C Modified: branches/gcc-4_2-branch/gcc/cp/ChangeLog branches/gcc-4_2-branch/gcc/cp/class.c branches/gcc-4_2-branch/gcc/testsuite/ChangeLog
Fixed in 4.2.0.
Subject: Bug 31449 Author: mmitchel Date: Tue Apr 10 06:07:41 2007 New Revision: 123691 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=123691 Log: PR c++/31449 * class.c (build_base_path): Ensure that the converted pointer has the same cv-qualification as the input. PR c++/31449 * g++.dg/init/const5.C: New test. Added: branches/gcc-4_1-branch/gcc/testsuite/g++.dg/init/const5.C Modified: branches/gcc-4_1-branch/gcc/cp/ChangeLog branches/gcc-4_1-branch/gcc/cp/class.c branches/gcc-4_1-branch/gcc/testsuite/ChangeLog
Fixed in 4.1.3.