g++ is not able to compile the following code: ---------------------------------------------------- #include <iostream> using namespace std; struct A { int f() {} }; struct B : A { }; int main() { int (A::*pa)() = &A::f; int (B::*pb)() = &B::f; cout << boolalpha << (pa == pb) << endl; } ---------------------------------------------------- ...but according to ISO (see 5.10/2) this code should compile and result of the pointer comparsion would be 'true'. Release: 3.1.1 Environment: Mandrake Linux
State-Changed-From-To: open->analyzed State-Changed-Why: I agree. In particular, the referenced part of the standard states: Pointer to member conversions(_conv.mem_) and qualification conversions (_conv.qual_) are performed to bring them to a common type. This does not seem to happen. (Conversion by hand yields the desired result though, so there is a way to work around this problem.) W. PS: As a sidenote (for the curious), the standard also says this on comparing pointers to member functions: if either [operand] is a pointer to a virtual member func- tion, the result is unspecified That seems like a trap you don't want to fall into... (And there isn't even a way for the compiler to warn you about.)
Here is a shorter one that can be able to put in gcc's testsuite: extern "C" void abort(); extern "C" void exit(int); struct A { int f() {} }; struct B : A { }; int main() { int (A::*pa)() = &A::f; int (B::*pb)() = &B::f; if(! (pa == pb)) abort(); exit(0); }
Just for more references into the standard: 4.11/2 describes the conversion from pointer-to-member-of-base to ptm-of-derived. 5.10/2 states that this conversion has to happen. Just out of curiosity: the conversion is really not all that trivial, since when you cast &B::f to &D::f you may need to use a thunk to adjust the this pointer, right? Or is this implemented by having a pointer-to- member-type be a tuple of pointer and this-adjustment? W.
PR 18040 shows a testcase where we ICE and the problem is related to this testcase.
Testing a patch.
Subject: Bug number PR c++/8171 A patch for this bug has been added to the patch tracker. The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2006-04/msg00861.html
Subject: Bug number PR c++/8171 A patch for this bug has been added to the patch tracker. The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2007-10/msg01789.html
Subject: Bug 8171 Author: aaw Date: Sat Dec 1 08:56:55 2007 New Revision: 130554 URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=130554 Log: PR c++/8171 gcc/cp/ * typeck.c (build_binary_op): Add conversion of pointers to function members appearing as operands to the equality operators. gcc/testsuite/ * g++.dg/conversion/ptrmem9.C: New test. Added: trunk/gcc/testsuite/g++.dg/conversion/ptrmem9.C Modified: trunk/gcc/cp/ChangeLog trunk/gcc/cp/typeck.c trunk/gcc/testsuite/ChangeLog
Fixed by revision 130554.