c++/4337: c++ ignores cast operator

serge.bogdanov@intel.com serge.bogdanov@intel.com
Mon Sep 17 12:06:00 GMT 2001


>Number:         4337
>Category:       c++
>Synopsis:       c++ ignores cast operator
>Confidential:   no
>Severity:       critical
>Priority:       high
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Sep 17 12:06:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     intel
>Release:        3.0.0 and 3.0.1
>Organization:
>Environment:

>Description:

In the code below the cast 'a1 = (A1)a2' should be resolved
through the explicit cast operator 'A2::operator A1(). 
The compiler silently ignores it and uses the path 
downcasting A2 to A and using the 'A1(const A &)' 
constructor.

If you remove the explicit casting (A1), it will complain
about an ambiguous conversion which I belive is wrong since
the explicit cast operator exixts:

a.cpp: In function `int main()':
a.cpp:26: warning: choosing `A2::operator A1()' over `A1::A1(const A&)'
a.cpp:26: warning:   for conversion from `A2' to `A1'
a.cpp:26: warning:   because conversion sequence for the argument is better

###################

#include <iostream.h>

struct A {
};
struct A1 : public A {

    A1(const A &) {
	cout << "A1(const A &)" << endl;
    }
    A1() {
	cout << "A1()" << endl;
    }
};
A1	X;
struct A2 : public A {
    operator A1() {
	cout << "operator A1()" << endl;
	return X;
    }
};

int main()
{
    A2 a2;
    A1 a1;
    a1 = (A1)a2;
}
>How-To-Repeat:
> g++ -o a a.cpp -Wall
> a

A1()
A1()
operator A1()
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list