Topic: Copy ctor elision


Author: T Gani <tgani@my-deja.com>
Date: 1999/12/30
Raw View
The first sentence of ISO C++ 12.8/15 (copying class objects) allows
elision of copy ctor calls when copying from a temporary. Consider the
following example:

struct X {
  int i;
  X(int ii) : i(ii) { }
  X(const X& x) : i(x.i+1) {}
};

X a(X(2));

Then, is the value of a.i 2 or 3, or is it implementation dependent
(i.e. is the copy ctor called at all)?  Intuitively one would expect it
to be 3; copy ctor elision appears to have been allowed solely for
eliminating needless copies when class objects are returned from
functions and its application here seems counter-intuitive. Is there a
defect report regarding this?

Thanks,

T. Gani.


Sent via Deja.com http://www.deja.com/
Before you buy.


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/12/31
Raw View
In article <84dqe1$97c$1@nnrp1.deja.com>, T Gani <tgani@my-deja.com>
writes
>The first sentence of ISO C++ 12.8/15 (copying class objects) allows
>elision of copy ctor calls when copying from a temporary. Consider the
>following example:
>
>struct X {
>  int i;
>  X(int ii) : i(ii) { }
>  X(const X& x) : i(x.i+1) {}
>};
>
>X a(X(2));
>
>Then, is the value of a.i 2 or 3, or is it implementation dependent
>(i.e. is the copy ctor called at all)?  Intuitively one would expect it
>to be 3; copy ctor elision appears to have been allowed solely for
>eliminating needless copies when class objects are returned from
>functions and its application here seems counter-intuitive. Is there a
>defect report regarding this?

If you choose to write a copy ctor that does not do strict copying that
is your problem.  The allowed elisions are not a version of the 'as-if'
rule they provide a constraint on what you, the programmer, may assume.



Francis Glassborow      Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Gabriel Dos_Reis <gdosreis@korrigan.inria.fr>
Date: 1999/12/31
Raw View
T Gani <tgani@my-deja.com> writes:

| The first sentence of ISO C++ 12.8/15 (copying class objects) allows
| elision of copy ctor calls when copying from a temporary. Consider the
| following example:
|
| struct X {
|   int i;
|   X(int ii) : i(ii) { }
|   X(const X& x) : i(x.i+1) {}
| };
|
| X a(X(2));
|
| Then, is the value of a.i 2 or 3, or is it implementation dependent
| (i.e. is the copy ctor called at all)?

It may even depend on optimization levels.

The sentence you're quoting does end with "even if the class copy
constructor or destructor have side effects."

| ...  Intuitively one would expect it
| to be 3; copy ctor elision appears to have been allowed solely for
| eliminating needless copies when class objects are returned from
| functions and its application here seems counter-intuitive.

User-defined copy constructor should not have counter-intuitive
semantics :-)

The moral is that you should not rely on side effects in
copy-constructor.  Copy-constructor should have value-semantics.

| ... Is there a
| defect report regarding this?

I don't think that is a defect.

--
Gabriel Dos Reis, dosreis@cmla.ens-cachan.fr


[ 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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]