Article View: comp.std.c++
Article #3733Re: Constructors and conversion operator
From: Roman Lechtchinsky
Date: Tue, 19 Mar 1996 10:24
Date: Tue, 19 Mar 1996 10:24
75 lines
3260 bytes
3260 bytes
Steve Clamage wrote:
>
> In article 3438@cs.tu-berlin.de, Roman Lechtchinsky
> <wolfro@cs.tu-berlin.de> writes:
> >
> >I have a question about conversion operators and constructors. Consider the
> >following:
> >
> >class X
> >{
> > X( Y );
> >};
> >
> >class Y
> >{
> > operator X();
> >};
>
> A constructor taking a single argument is a type conversion operator from
> the argument type to the class type. The arrangement you show is always
> going to lead to ambiguity errors. There are two ways to convert a Y
> to an X, and neither is preferred over the other. Operator conversion
> functions should normally be avoided, partly for this reason, and partly
> because they can wind up being used in surprising circumstances. The
> compiler might find a conversion that allows erroneous code to compile.
>
> >Now the second one:
> >
> >class A
> >{
> > A(B);
> >};
> >
> >class B
> >{
> > operator A&();
> >};
>
> This is the same situation. There is no syntactic difference between
> using a reference or an object. Once again, there are two ways to
> make an A out of a B, and neither is preferred.
>
First of all, sorry: there is a mistake in the example, the constructor
should be A(B&). However, it doesn't matter since both cases are only a
variation of the following problem:
The DWP defines:
A constructor declared without the function-specifier explicit that
can be called with a single parameter specifies a conversion from the
type of its first parameter to the type of its class. [class.conv.ctor]
Now, to me this means that a copy constructor is a converting constructor
since it takes a single parameter which is a reference to an object of the
class ( I don't think that this is intended ). If this is true a copy
constructor is a user-defined conversion ( after all, A and A& are different
types, aren't they ). This implies that the following call to foo:
foo(b)
is ill-formed ( not ambiguous ) since it requires either b=>B(b)=>A(B(b)) or
b=>A&(b)=>A(A&(b)) but only one user-defined conversion may be applied ( note
that foo is defined as foo(A), not foo(A&) ).
Bye
Roman
---
[ 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 ]
Technical Details
Message-ID:
<314E1D5D.76E5@cs.tu-berlin.de>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<314CD29A.3438@cs.tu-berlin.de> <4ikkp8$b5u@engnews1.Eng.Sun.COM>Show Full Headers
Complete Article Headers
From: Roman Lechtchinsky <wolfro@cs.tu-berlin.de> Message-ID: <314E1D5D.76E5@cs.tu-berlin.de> X-Original-Date: Tue, 19 Mar 1996 03:35:09 +0100 Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 19 Mar 96 10:24:48 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: Re: Constructors and conversion operator Organization: Technical University of Berlin References: <314CD29A.3438@cs.tu-berlin.de> <4ikkp8$b5u@engnews1.Eng.Sun.COM> X-Mailer: Mozilla 2.0 (Win95; I) X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMU6LeeEDnX0m9pzZAQGNSQF+OtaCiBobZZlR0UUd9/RPcuu5CryL6KfL A4SIuO1eUcllMakDAjyYB2EhEfUwn0tH =XONZ