Bug 8171 - Cannot compare pointer to member function of base and derived class
Summary: Cannot compare pointer to member function of base and derived class
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.1.1
: P3 normal
Target Milestone: 4.3.0
Assignee: Volker Reichelt
URL:
Keywords: monitored, rejects-valid
Depends on:
Blocks: 18040
  Show dependency treegraph
 
Reported: 2002-10-08 11:06 UTC by zyzstar
Modified: 2007-12-01 09:08 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-09-03 21:39:45


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description zyzstar 2002-10-08 11:06:00 UTC
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
Comment 1 Wolfgang Bangerth 2002-11-04 08:15:06 UTC
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.)
Comment 2 Andrew Pinski 2003-07-22 19:00:09 UTC
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);
}
Comment 3 Wolfgang Bangerth 2003-07-22 20:23:16 UTC
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.
Comment 4 Andrew Pinski 2004-11-06 03:17:48 UTC
PR 18040 shows a testcase where we ICE and the problem is related to this testcase.
Comment 5 Volker Reichelt 2006-04-21 20:09:22 UTC
Testing a patch.
Comment 6 patchapp@dberlin.org 2006-04-23 01:15:15 UTC
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
Comment 7 patchapp@dberlin.org 2007-10-30 07:20:26 UTC
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
Comment 8 Ollie Wild 2007-12-01 08:57:09 UTC
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

Comment 9 Ollie Wild 2007-12-01 09:08:38 UTC
Fixed by revision 130554.