Article View: comp.std.c++
Article #3753Re: Constructors and conversion operator
From: Roman Lechtchinsky
Date: Thu, 21 Mar 1996 07:48
Date: Thu, 21 Mar 1996 07:48
67 lines
4089 bytes
4089 bytes
Steve Clamage wrote:
>
> In article 76E5@cs.tu-berlin.de, Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
> writes:
> >
> >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 ).
>
> I don't think it matters. The conversion is the identity conversion. In
> places where conversions are required, there is almost always more than
> one way to achieve the conversion. (Often there is an infinite number
> of possible conversion sequences, such as short->int->short->int->
> short->...->int.) The explicit rule in such cases is to choose the
> shortest possible sequence of conversions.
>
> Suppose a copy constructor T::T(const T&) is viewed as a conversion
> from T to T. If such a conversion is "needed", the shortest conversion
> sequence must be chosen. That would be the null sequence, and so the
> copy ctor would never be used or even considered for that purpose.
> (Just as the sequence int->Rational->float would never be considered in
> converting an int to a float.)
I'm not too sure here. First, if the copy constructor is the identity
conversion, why is it called at all? I mean, if the identity conversion is
eliminated from the conversion sequence, why isn't the call to the copy
constructor itself eliminated? Second, if the copy constructor is called, why
is it called only once? Consider
class A { A(const A&); };
void foo(A);
A a;
Now, foo(a) is implicitly converted to foo(A(a)). Why not to foo(A(A(a))?
Clearly, if the copy constructor is a conversion which has to be performed on
a, it *is* performed and it is performed only once, since no conversion is
needed thereafter. If the copy constructor is not a conversion, things get
complicated.
Actually, I can't agree that the copy constructor is the identity conversion.
It is a conversion from a reference to the referenced type, i.e. from A& to A
in this case. A& and A are two different types and the DWP doesn't say that a
conversion from A& to A (i.e. the copy constructor) isn't a conversion or is
to be eliminated from the conversion sequence or whatever... It just isn't
stated explicitly, probably because everybody knows it. I think this is the
problem.
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 ]
---
[ 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:
<31505061.2E03@cs.tu-berlin.de>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<314E1D5D.76E5@cs.tu-berlin.de> <4imnnl$j29@engnews1.Eng.Sun.COM>Show Full Headers
Complete Article Headers
From: Roman Lechtchinsky <wolfro@cs.tu-berlin.de> Message-ID: <31505061.2E03@cs.tu-berlin.de> X-Original-Date: Wed, 20 Mar 1996 19:37:21 +0100 Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 21 Mar 96 07:48:34 GMT Approved: fjh@cs.mu.oz.au Return-Path: <daemon@meeker.UCAR.EDU> Newsgroups: comp.std.c++ Subject: Re: Constructors and conversion operator Organization: Technical University of Berlin References: <314E1D5D.76E5@cs.tu-berlin.de> <4imnnl$j29@engnews1.Eng.Sun.COM> X-Mailer: Mozilla 2.0 (Win95; I) X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMVEJ/uEDnX0m9pzZAQFNzgF/a32DUPua7gkyULne2dcBVFh31T4QDS2e Pf8f1hNLPqxElrU1g8zUwxjga1EWr3SS =wC2d