Topic: Ambiguity when overloading operators
Author: Martin Aupperle <100754.2730@CompuServe.COM>
Date: 1996/04/11 Raw View
If I define
struct X {
X( int );
operator const char* () const;
/* other members */
};
X& operator + ( const X&, const X& );
I cannot say
X x1( 5 );
X x2 = x1+3; // ambiguous
Borland BC4.5 says that x1+3 is ambiguous. I know that it can
1. convert 3 to an X and call operator + ( const X&, constX& )
2. convert x1 to a const char* and do pointer arithmetics.
Is the compiler right? I have in mind that a conversion towards a user defined
type has precedence over the conversion to a fundamental type. So choice (1)
should be right and it shoud not be ambiguous.
Am I correct?
Martin
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/04/12 Raw View
In article 1@CompuServe.COM, Martin Aupperle <100754.2730@CompuServe.COM> write
s:
> struct X {
> X( int );
> operator const char* () const;
> };
> X& operator + ( const X&, const X& );
>I cannot say
> X x1( 5 );
> X x2 = x1+3; // ambiguous
>Borland BC4.5 says that x1+3 is ambiguous. I know that it can
>1. convert 3 to an X and call operator + ( const X&, constX& )
>2. convert x1 to a const char* and do pointer arithmetics.
Yes. Each choice involves exactly one user-defined conversion,
and no user-defined conversion is preferred over any other.
The compiler is correct.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: b91926@fsgi01.fnal.gov (David Sachs)
Date: 1996/04/12 Raw View
Martin Aupperle <100754.2730@CompuServe.COM> writes:
>If I define
> struct X {
> X( int );
> operator const char* () const;
> /* other members */
> };
> X& operator + ( const X&, const X& );
>I cannot say
> X x1( 5 );
> X x2 = x1+3; // ambiguous
>Borland BC4.5 says that x1+3 is ambiguous. I know that it can
>1. convert 3 to an X and call operator + ( const X&, constX& )
>2. convert x1 to a const char* and do pointer arithmetics.
>Is the compiler right? I have in mind that a conversion towards a user defined
>type has precedence over the conversion to a fundamental type. So choice (1)
>should be right and it shoud not be ambiguous.
The compiler is right. The expression x1+3 is treated as if it were
a function call: operator+(x1,3). There are 2 relevant candidates.
1) operator+(const X&, const X&)
2) operator+(char* const, const int) (built-in pointer arithmetic)
The first form is a better match for the first argument. The second
for is a better match for the second argument. Therefore the construct
is ambiguous.
If you had an operator+(const X&, const int), it would be a best match
and would be used.
--
***** The stories about the first lady are hilarious. *****
David Sachs - Fermilab, HPPC MS369 - P. O. Box 500 - Batavia, IL 60510
Voice: 1 708 840 3942 Deparment Fax: 1 708 840 3785
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]