Article View: comp.std.c++
Article #2312Re: Q: is A a = 100; equivalent to A a (100); ?
From: J. Kanze
Date: Thu, 26 Oct 1995 14:58
Date: Thu, 26 Oct 1995 14:58
76 lines
3487 bytes
3487 bytes
Alex Martelli (martelli@cadlab.cadlab.it) wrote:
|> matt@godzilla.EECS.Berkeley.EDU (Matt Austern) writes:
|> ...
|> >Sort of. If a is of class A, if the copy constructor X::X(const X&)
|> >exists, and if the converting constructor X::X(const A&) exists and
|> >isn't qualified as explicit, then "X x(a)" simply invokes the
|> >converting constructor while "X x=a" invokes the converting
|> >constructor and then the copy constructor.
|> So, to clarify my understanding -- the following program...:
|> --cut tc.cc
|> #include <iostream.h>
|> class A { };
|> class X {
|> public:
|> X(const X&) {
|> cout<<"X::copy ctor"<<endl;
|> }
|> X(const A&) {
|> cout<<"X::ctor(A&)"<<endl;
|> }
|> };
|> int
|> main()
|> {
|> A a;
|> X x1 = a;
|> X x2(a);
|> return 0;
|> }
|> --end cut
|> ... should necessarily emit:
|> X::ctor(A&)
|> X::ctor(A&)
|> X::copy ctor
|> and an implementation optimizing out the copy ctor in this case
|> would be non-conforming...? Oh boy, I _am_ confused -- I thought
|> such sentences in the draft as "Either direct-initialization semantics
|> or copy-initialization semantics apply" in [class.expl.init] sub 1
|> were there to specifically allow such optimizing-out... perhaps it
|> would be beneficial to the standard's clarity if this was spelled
|> out in as many words (or IS it there, and I'm just being unable to
|> see it? I still think some clarification would help...).
An implementation is authorized to optimize out the copy constructor, so
there are two possible outcomes: the above, and the same without the
copy constructor.
The confusion may be stemming from the fact that even if the compiler
does not call the copy constructor, it must evaluate the program (for
legality, etc.) as if it did. So that even if the compiler optimized
out the call to the copy constructor, the program would still be illegal
if, for example, the copy constructor were private.
(There is an interesting question here. Suppose that the copy
constructor is declared public, but no implementation is given. I think
that under the current rules, the compiler is required to give a
diagnostic. In practice, I'm willing to bet that none of the compilers
which optimize out the copy constructor do.)
--
James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle--
--Beratung in industrieller Datenverarbeitung
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<46nnao$rve@gabi.gabi-soft.fr>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamageReferences:
<damian.813960961@molly.cs.monash.edu.au> <4638jp$ef4@fsgm01.fnal.gov> <MATT.95Oct18111956@godzilla.EECS.Berkeley.EDU> <martelli.814175813@cadlab>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage From: kanze@gabi-soft.fr (J. Kanze) Newsgroups: comp.std.c++ Subject: Re: Q: is A a = 100; equivalent to A a (100); ? Date: 26 Oct 1995 14:58:11 GMT Organization: GABI Software, Sarl. Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <46nnao$rve@gabi.gabi-soft.fr> References: <damian.813960961@molly.cs.monash.edu.au> <4638jp$ef4@fsgm01.fnal.gov> <MATT.95Oct18111956@godzilla.EECS.Berkeley.EDU> <martelli.814175813@cadlab> NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Newsreader: TIN [version 1.2 PL2] Content-Length: 2550 X-Lines: 72 Originator: clamage@taumet