Casting Overloaded Function Pointer Problem...Repost...
Don Mies
dmies@bluekite.com
Mon Sep 18 11:06:00 GMT 2000
ÃÂ
Gentlemen,
This is a reposting of an earlier request.ÃÂ I received no responses
that helped in resolving my problem so I will try posting the query
again...
I have run into a problem trying to use the g++ compiler on a Sun
Solaris 7 system.
I've been trying to compile a version of the Microsoft MFC library
with the g++ compiler and have run into a problem that I don't know
how to resolve.
Here is a highly condensed test file that illustrates the problem.
--------------------------------------------
ÃÂ ÃÂ ÃÂ ÃÂ 1
ÃÂ ÃÂ ÃÂ ÃÂ 2ÃÂ class
CCmdTarget;
ÃÂ ÃÂ ÃÂ ÃÂ 3ÃÂ class
CWnd;
ÃÂ ÃÂ ÃÂ ÃÂ 4ÃÂ class
CRecordView;
ÃÂ ÃÂ ÃÂ ÃÂ 5
ÃÂ ÃÂ ÃÂ ÃÂ 6ÃÂ typedef
int (CCmdTarget::*PMSG)(int);
ÃÂ ÃÂ ÃÂ ÃÂ 7
ÃÂ ÃÂ ÃÂ ÃÂ 8ÃÂ class
CCmdTarget {};
ÃÂ ÃÂ ÃÂ ÃÂ 9
ÃÂ ÃÂ ÃÂ 10
ÃÂ ÃÂ ÃÂ 11ÃÂ class
CWnd : public CCmdTarget
ÃÂ ÃÂ ÃÂ 12ÃÂ {
ÃÂ ÃÂ ÃÂ 13ÃÂ ÃÂ ÃÂ
public:
ÃÂ ÃÂ ÃÂ 14ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
int OnMove (int x, int y);
ÃÂ ÃÂ ÃÂ 15ÃÂ };
ÃÂ ÃÂ ÃÂ 16
ÃÂ ÃÂ ÃÂ 17
ÃÂ ÃÂ ÃÂ 18ÃÂ class
CRecordView : public CWnd
ÃÂ ÃÂ ÃÂ 19ÃÂ {
ÃÂ ÃÂ ÃÂ 20ÃÂ ÃÂ ÃÂ
public:
ÃÂ ÃÂ ÃÂ 21ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
int OnMove (int);
ÃÂ ÃÂ ÃÂ 22ÃÂ ÃÂ ÃÂ
protected:
ÃÂ ÃÂ ÃÂ 23ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
void OnMove (int cx, int cy);
ÃÂ ÃÂ ÃÂ 24ÃÂ ÃÂ ÃÂ
private:
ÃÂ ÃÂ ÃÂ 25ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
static const PMSG _messageEntries[];
ÃÂ ÃÂ ÃÂ 26ÃÂ };
ÃÂ ÃÂ ÃÂ 27
ÃÂ ÃÂ ÃÂ 28
ÃÂ ÃÂ ÃÂ 29ÃÂ ////////////////////////////////////////////////////////////
ÃÂ ÃÂ ÃÂ 30
ÃÂ ÃÂ ÃÂ 31ÃÂ const
PMSG CRecordView::_messageEntries[] =
ÃÂ ÃÂ ÃÂ 32ÃÂ {
ÃÂ ÃÂ ÃÂ 33ÃÂ ÃÂ ÃÂ ÃÂ ÃÂ
(PMSG)&CRecordView::OnMove
ÃÂ ÃÂ ÃÂ 34ÃÂ };
ÃÂ ----------------------------------------
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
--ÃÂ
This e-mail contains information intended only for the
use of the individual or entity named above.ÃÂ If the reader of this
e-mail is not the intended recipient or the employee or agent responsible
for delivering it to the intended recipient, any dissemination, publication
or copy of this e-mail is strictly prohibited.ÃÂ If you have received
this e-mail in error, please immediately notify the sender.
Thank you.
ÃÂ
begin:vcard
n:Mies;Don
tel;fax:(714) 375-2737
tel;work:(714) 843-6050
x-mozilla-html:FALSE
url: http://www.bluekite.com
org:BlueKite.com, Inc.;Unix Development
version:2.1
email;internet:dmies@bluekite.com
adr;quoted-printable:;;17011 Beach Blvd, Suite 1230=0D=0A;Huntington Beach;CA;92647;U.S.A.
x-mozilla-cpt:;-24136
fn:Don Mies
end:vcard
More information about the Gcc-bugs
mailing list