ambiguous overload in 2.8.1 and 2.91.60

Hans Werner Strube strube@physik3.gwdg.de
Thu Feb 4 10:01:00 GMT 1999


When a cast from a class to a previously defined class is defined and
there is also an indirect cast path via a numeric type, both g++ 2.8.1
and egcs-2.91.60 give an error like
tt.C: In function `class myfloat sub(const class str16 &)':
tt.C:21: call of overloaded `myfloat(const str16 &)' is ambiguous
tt.C:9: candidates are: myfloat::myfloat(const myfloat &)
tt.C:6:                 myfloat::myfloat(double)
The direct cast is not even mentioned here, and it is not clear why the
myfloat::myfloat(const myfloat &) should be considered at all.
This worked in all g++ versions before 2.8 and also works with the Sun
and SGI CC compilers. Below a little test program:

#include <stdlib.h> //atof()

class myfloat {
public:
myfloat(double d) {f=(float)d;}
private:
float f;
};

class str16 {
public:
operator myfloat() const { return (myfloat)atof(cc); }
operator double() const {return atof(cc);}
private:
char cc[16];
};

myfloat sub(const str16 &s)
{
	myfloat mf(s);
	return mf;
}

Of course, here I could reorder the classes and specify a constructor
myfloat(const str16 &) instead of the operator myfloat(). But if the
first class cannot be changed (for instance, in a given header file)
this workaround is impossible. Is this a new feature or a bug? And
what should I do?

-- 

 Hans Werner Strube          strube@physik3.gwdg.XPAM.de (remove .XPAM)
 Drittes Physikalisches Institut,  Univ. Goettingen
 Buergerstr. 42-44,   D-37073 Goettingen,   Germany


More information about the Gcc-bugs mailing list