[comp.lang.c++] user-defined conversion--bug or error?

tuck@jason.cs.unc.edu (Russ Tuck) (12/07/89)

The example program below defines classes A and B.  A has operator+, and
B has a user-defined conversion to A.  The question is whether it is legal
to use A's + on B's objects, via the B->A conversion.

GNU g++ 1.36.1 says yes, but AT&T cfront 2.0 says no.  Which is right?
I think the code is legal C++.  Am I wrong?  If so, please explain why.
 
tuck@piglet> cat type2ab.C
class A {
 public:
  friend const A operator+ (const A& lsrc, const A& rsrc);
};

class B {
 public:
  B();
  operator const A&();
};

main()
{
  A a0;
  B b0, b1;

  a0 = b0 + b1;   // legal???
}

tuck@piglet> g++ -v -c type2ab.C
gcc version 1.36.1 (based on GCC 1.36)
 /usr/softlab/contrib/lib/m68k_sunos/gcc-cpp -+ -v -undef -D__GNUC__ -D__GNUG__ -D__cplusplus -Dmc68000 -Dsun -Dunix -D__mc68000__ -D__sun__ -D__unix__ -Dmc68010 type2ab.C /usr/tmp/cca07794.cpp
GNU CPP version 1.36
 /usr/softlab/contrib/lib/m68k_sunos/gcc-cc1plus /usr/tmp/cca07794.cpp -quiet -dumpbase type2ab.C -version -o /usr/tmp/cca07794.s
GNU C++ version 1.36.1 (based on GCC 1.36) (68k, MIT syntax) compiled by GNU C version 1.36.
default target switches:
 /usr/softlab/contrib/lib/m68k_sunos/gcc-as -mc68010 -o type2ab.o /usr/tmp/cca07794.s
tuck@piglet> CC.new -c type2ab.C
CC  type2ab.C:
"type2ab.C", line 17: error: ambiguous use of overloaded +:  B  and  B 
1 error
tuck@piglet> 

piglet is a Sun-3/60 running SunOS 4.0.3.

Russ Tuck		               tuck@cs.unc.edu
UNC Dept. of Computer Science          ...!mcnc!unc!tuck
CB 3175, Sitterson Hall
Chapel Hill, NC 27599-3175, USA        (919) 962-1755 or 962-1700

tuck@jason.cs.unc.edu (Russ Tuck) (12/07/89)

Simplifying my earlier program slightly gave interesting results.
First, I removed all the "const" keywords, to make it legal C++ 1.2.
With this change, AT&T cfront 1.2 compiled it without error (but 2.0
gave the same error).

Then I deleted the "&" chars, so + and conversions use valuess instead
of references.  This finally compiled without error with cfront 2.0
(as well as 1.2 and g++).

This suggests that the problem is in cfront 2.0.  Any comments?
Suggestions??

Here are the details...
CC.new = AT&T cfront 2.0
CC     = AT&T cfront 1.2
g++    = GNU g++ 1.36.1
piglet = Sun-3/60, SunOS 4.0.3

(no const, keep ref &)
tuck@piglet> cat type2ac.C
class A {
 public:
  friend A operator+ (A& lsrc, A& rsrc);
};

class B {
 public:
  B();
  operator A&();
};

main()
{
  A a0;
  B b0, b1;

  a0 = b0 + b1;    // legal???
}

tuck@piglet> CC.new -c type2ac.C
CC  type2ac.C:
"type2ac.C", line 17: error: ambiguous use of overloaded +:  B  and  B 
1 error
tuck@piglet> CC -c type2ac.C
tuck@piglet> g++ -c type2ac.C
tuck@piglet> 

(no const, no ref &)
tuck@piglet> cat type2ad.C
class A {
 public:
  friend A operator+ (A lsrc, A rsrc);
};

class B {
 public:
  B();
  operator A();
};

main()
{
  A a0;
  B b0, b1;

  a0 = b0 + b1;    // legal???
}

tuck@piglet> CC.new -c type2ad.C
CC  type2ad.C:
cc  -c  type2ad.c
tuck@piglet> CC -c type2ad.C
tuck@piglet> g++ -c type2ad.C
tuck@piglet> 

jimad@microsoft.UUCP (Jim Adcock) (12/14/89)

Changing the a0 = b0 + b1 to a0 = operator+(b0,b1) makes 2.0 accept it,
which seems to imply compiler bug to me.