[comp.lang.c++] supposedly ambiguous conversion -- Summary

cok@islsun.Kodak.COM (David Cok) (11/08/90)

Thanks to those who replied to the referenced posting.  A copy of the original
(with one typo corrected) is appended at the end of this message.

The original problem was that in setting up a hierarchy of classes with
implicit conversions (Byte->Int->Float->Double), the Sun C++ 2.0 compiler
complained about ambiguities in resolving overloaded binary operations between
Byte and Int.  This happened if (1) all six conversions were defined with
constructors of the form Double::Double(Byte), or (2) all six conversions were
defined with operators as in Byte::operator Double().

The essence of expert opinion was that this is a bug and will work in future
compilers.  However one responder happened on a workaround.  If the classes
are declared in the order Int, Double, Float, Byte (instead of my original
order Double, Float, Int, Byte), the compiler works ok.  Using a well
chosen mix of constructors and operators also worked.  These are hacks to be
sure but they will carry me through 2.0.

David R. Cok
Eastman Kodak Company
e-mail: cok@Kodak.com

Original posting:


Consider this program, which gives compilation errors.  It is boiled out of
a larger more useful context.

class Byte;
class Int;
class Float;

class Double { 
    public:
	double v;

	Double();
	Double(const Double&);

	Double(Byte);
	Double(Int);
	Double(Float);

	friend Double operator + (Double a, Double b);
};

class Float { 
    public:
	float v;

	Float();
	Float(const Float&);

	Float(Int);
	Float(Byte);

	friend Float operator + (Float a, Float b);
};

class Int { 
    public:
	int v;

	Int();
	Int(const Int&);

	Int(Byte);

	friend Int operator + (Int a, Int b);
};

class Byte { 
    public:
	unsigned char v;

	Byte();
	Byte(const Byte&);

	friend Byte operator + (Byte a, Byte b);
};


main()
{
	Byte 	b;
	Int	i;

	b+i;
}


Sun C++ 2.0 on a SparcStation 1 says the following:

CC  ambiguity.c:
"ambiguity.c", line 64: error: ambiguous argument for operator +():  void (Double , Double ) and  void (Float , Float )
"ambiguity.c", line 64: warning: result of + expression not used
1 error

(Line 64 is the b+i expression.)

The warning is obvious, but I do not understand the error.  Why won't the 
compiler choose to convert variable b to Int and then use the operator+ for
Ints?  The second argument is an exact match for an Int.

If I replace the constructors (like Double::Double(Byte);) with operators 
(like Byte::operator Double()), the problem is the same.

Will anyone give me some advice?
Please respond via e-mail (and to the net if you wish, of course).

David R. Cok, Eastman Kodak Company, 716-477-7086
e-mail: cok@Kodak.COM