Article View: comp.std.c++
Article #2210Re: Q: is A a = 100; equivalent to A a (100); ?
From: Steve Clamage
Date: Mon, 16 Oct 1995 04:00
Date: Mon, 16 Oct 1995 04:00
41 lines
2023 bytes
2023 bytes
damian@molly.cs.monash.edu.au (Damian Conway) writes: >jason@cygnus.com (Jason Merrill) and other cogniscenti write: >>> | According to the standard, are the two lines of the main >>> | equivalent ? My Opinion is yes >>No. >Then is it possible to set up a class X wherein: > X x(1); >and > X x = 1; >behave differently? Yes. Silly example: Write a message to the terminal from the copy constructor. If the copy is optimized away, you won't see the message. >Or is the compiler always allowed to optimize away the temporary and copy >constructor, even if that changes the semantics of the program? In the second example, the compiler is allowed but not required to optimize away the copy. That is, when you write X x = 1; it can be interpreted as either X x(1); or X x(X(1)); This is only one of many situations where you cannot predict whether a temporary will be generated and the copy constructor used. For that reason, you should write copy constructors so that program correctness doesn't depend on whether they are optimized away. -- Steve Clamage, stephen.clamage@eng.sun.com [ 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:
<45sl3a$coc@engnews2.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamageReferences:
<JLM.95Oct12202250@ludo.two-oo-one.fr> <u9burle2pv.fsf@yorick.cygnus.com> <45skdo$cm4@engnews2.Eng.Sun.COM>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews2.Eng.Sun.COM!taumet!clamage From: clamage@Eng.sun.com (Steve Clamage) Newsgroups: comp.std.c++ Subject: Re: Q: is A a = 100; equivalent to A a (100); ? Date: 16 Oct 1995 04:00:33 GMT Organization: Sun Microsystems Inc., Mountain View, CA Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <45sl3a$coc@engnews2.Eng.Sun.COM> References: <JLM.95Oct12202250@ludo.two-oo-one.fr> <u9burle2pv.fsf@yorick.cygnus.com> <45skdo$cm4@engnews2.Eng.Sun.COM> NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Nntp-Posting-Host: taumet.eng.sun.com X-Newsreader: NN version 6.5.0 #21 (NOV) Content-Length: 1097 Originator: clamage@taumet