Topic: Is this example of overloading ambiguous?


Author: mlg@scr.siemens.com (Michael Greenberg)
Date: 1997/04/23
Raw View

[I posted this to comp.c++.moderated and received no responses]

Which constructor of String should be invoked at the marked line?  Is
it ambiguous?  Why? (I've gotten three different answers with four
different compilers!)

Would it make a difference if
   Path::operator String
was changed to
   Path::operator String&
or
   Path::operator const String&




#include <stdio.h>

class String {
 public:
  String(const char*){ printf("ctor-1\n"); }
  String( const String&) { printf("ctor-2\n");}
} ;

String Z("foo") ;

class Path {
 public:
  operator const char*() const { printf("cnvn-1\n"); return "pqr";}
  operator String() const { printf("cnvn-2\n"); return Z; }
};

int
main()
{
  Path xx;
  String s(xx);     // What should happen here?

  printf("\n done.\n");
  return 0;
}

Thanks,

--
Michael Greenberg                      email: mgreenberg@scr.siemens.com
Siemens Corporate Research             phone: 609-734-3347
755 College Road East                  fax: 609-734-6565
Princeton, NJ 08540
---
[ 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: John Lilley <jlilley@empathy.com>
Date: 1997/04/24
Raw View
Michael Greenberg wrote:
>
> Which constructor of String should be invoked at the marked line?  Is
> it ambiguous?

> class String {
>  public:
>   String(const char*){ printf("ctor-1\n"); }
>   String( const String&) { printf("ctor-2\n");}
> } ;
>
> class Path {
>  public:
>   operator const char*() const { printf("cnvn-1\n"); return "pqr";}
>   operator String() const { printf("cnvn-2\n"); return Z; }
> };
>
> int main() {
>   Path xx;
>   String s(xx);     // What should happen here?

Methinks it is ambiguous.  Both constructors require a single
user-defined conversion.  The qualification adjustment need to go from
String to const String is considered an exact match, as is the
value-to-reference conversion needed to go from const String to const
String&.  So both conversions involve only a user-defined converion, and
are identically valid.

john lilley
---
[ 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: Alexandre Oliva <oliva@dcc.unicamp.br>
Date: 1997/04/28
Raw View
Michael Greenberg writes:

> Which constructor of String should be invoked at the marked line?  Is
> it ambiguous?  Why? (I've gotten three different answers with four
> different compilers!)

This is ambiguous, AFAIK.

There are two possible ways of converting Path to an argument to a
String constructor, both are user defined conversions, and none of
them is better than the other, because user defined conversion
sequences are only comparable if they both contain the same
user-defined conversion function or constructor.  Since this is not
the case, the conversion is ambiguous.

> Would it make a difference if
>    Path::operator String
> was changed to
>    Path::operator String&
> or
>    Path::operator const String&

Nope, there would still be two possible user defined conversion
functions to use.

--
Alexandre Oliva
mailto:oliva@dcc.unicamp.br mailto:aoliva@acm.org
Universidade Estadual de Campinas, SP, Brasil
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]