🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by Martin Aupperle Thu, 11 Apr 1996 23:50
Ambiguity when overloading operators
#3997
Author: Martin Aupperle
Date: Thu, 11 Apr 1996 23:50
35 lines
1483 bytes
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                             ]
Re: Ambiguity when overloading operators
#3996
Author: Steve Clamage
Date: Fri, 12 Apr 1996 07:33
32 lines
1659 bytes
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                             ]
Re: Ambiguity when overloading operators
#4018
Author: David Sachs
Date: Fri, 12 Apr 1996 20:17
51 lines
2354 bytes
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                             ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads