This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Casting Overloaded Function Pointer Problem...Repost...


Don Mies wrote:
> 
...

I did some research into this as our test suite was affected by the same
problem and it appears that your are essentially correct in expecting
the code to compile (edg, SunPro, and VisualAge all accept it). 5.2.9,
p9 allows the cast but specifies undefined behavior since the base
doesn't contain the member.

I agree that this is a g++ bug (a warning/remark in -W -pedantic mode
might still be appropriate). I would suggest you enter it in GNATS since
bug reports being sent to gcc-bugs don't always seem to make it there
(and it is apparently the process to follow). A reduced test case is
below.

Regards
Martin

class A { }; 

struct B : public A
{ 
    void foo (); 
    void foo (int);
}; 


int main ()
{
    void (A::*f)() = (void (A::*)())&B::foo;
}

> 
> If I compile the above with the Sun CC compiler, I get no complaints
> or warnings.  When I compile it with g++ (gcc version 2.95.2 19991024
> (release)) I get the following error messages:
> 
>     t.C:34: no matches converting function `OnMove' to type `int
> (CCmdTarget::*)(int)'
>     t.C:21: candidates are: int CRecordView::OnMove(int)
>     t.C:23:                 void CRecordView::OnMove(int, int)
> 
> If I comment out the overloaded function declaration at line 23, it will
> compile
> with no errors.
> 
> The compiler appears to be ignoring the cast operator at line 33 when it
> determines
> the function signature.
> 
> I received one response to my initial posting with the following
> information:
> 
> > Your code is essentially asking GCC to convert the pointer
> > to the member function into a p-to-m-f of the base class.
> > If I'm interpreting this correctly, according to the C++
> > Standard, section 13.4, this is not allowed:
> >
> > [Note: there are no standard conversions (clause 4) of one
> > pointer-to-function type into another. In particular, even
> > if B is a public base of D, we have
> >
> >       D* f();
> >       B* (*p1)() = &f; // error
> >       void g(D*);
> >       void (*p2)(B*) = &g; // error
> >
> > -end note]
> >
> > Therefore, I think GCC may be right in complaining about
> > this code.
> 
> However, I don't believe the above interpretation is incorrect as
> my example program above is doing an explicit cast and not
> a standard cast as the above response contends.  In addition,
> if I comment out line 23 to remove the overloading function
> declaration, it will compile with no errors and the casting
> operation is still in effect and unchanged.
> 
> Sections 5.1.10.6, 5.2.10.9, and 5.4.5 of the C++ Standard seem
> to indicate that the above construct should be legal but the g++
> compiler appears to disagree.
> 
> Is there a way to fix this without making major changes to the source
> files
> (I don't understand  the MFC library well enough to make changes to it)?
> 
> Thank you,
> 
> Don Mies
>

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]