Topic: elision of copy constructor and side effects


Author: Jonathan Mcdougall <jonathanmcdougall@gmail.com>
Date: Wed, 10 Feb 2010 11:51:14 CST
Raw View
> I think you misunderstood something. It's not that the standard allows
> compilers to ignore the code inside copy ctors. It's that it allows
> compilers to REMOVE THE NEED to execute copy ctors and dtors.

That makes sense, I was clearly reading that backwards. Thanks!

--
Jonathan Mcdougall

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Jonathan Mcdougall <jonathanmcdougall@gmail.com>
Date: Thu, 4 Feb 2010 18:43:04 CST
Raw View
12.8/15 says: "When certain criteria are met, an implementation is
allowed to omit the copy construction of a class object, even if the
copy constructor and/or destructor for the object have side effects."

For an example such as:

 #include <iostream>

 class A
 {
 public:
   A()
   {
   }

   A(const A&)
   {
     ++s;
   }

   static int s;
 };

 int A::s(0);

 A f()
 {
   A a;
   return a;
 }

 int main()
 {
   A a = f();
   std::cout << a.s;
 }

A quick test in Visual C++ 2008 shows "0" with optimizations enabled.
What is the output considered: Is it well-defined or unspecified? Is
there any mention in the standard (I couldn't find any) that says
whether side-effects in copy constructors have well-defined or
unspecified behavior?

--
Jonathan Mcdougall

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Mathias Gaunard <loufoque@gmail.com>
Date: Fri, 5 Feb 2010 13:37:49 CST
Raw View
On 5 f=E9v, 00:43, Jonathan Mcdougall <jonathanmcdoug...@gmail.com>
wrote:
> 12.8/15 says: "When certain criteria are met, an implementation is
> allowed to omit the copy construction of a class object, even if the
> copy constructor and/or destructor for the object have side effects."
>
> For an example such as:
>
> <snip example />
>
> A quick test in Visual C++ 2008 shows "0" with optimizations enabled.
> What is the output considered: Is it well-defined or unspecified?

It is unspecified whether the compiler omits copy constructors or not,
since it is allowed but not required.

Here, the value could either be 0, 1, or 2 depending on the compiler
and its settings.


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Francis Glassborow <francis.glassborow@btinternet.com>
Date: Fri, 5 Feb 2010 13:36:29 CST
Raw View
Jonathan Mcdougall wrote:

> 12.8/15 says: "When certain criteria are met, an implementation is
> allowed to omit the copy construction of a class object, even if the
> copy constructor and/or destructor for the object have side effects."
>
> For an example such as:
>
>  #include <iostream>
>
>  class A
>  {
>  public:
>   A()
>   {
>   }
>
>   A(const A&)
>   {
>     ++s;
>   }
>
>   static int s;
>  };
>
>  int A::s(0);
>
>  A f()
>  {
>   A a;
>   return a;
>  }
>
>  int main()
>  {
>   A a = f();
>   std::cout << a.s;
>  }
>
> A quick test in Visual C++ 2008 shows "0" with optimizations enabled.
> What is the output considered: Is it well-defined or unspecified? Is
> there any mention in the standard (I couldn't find any) that says
> whether side-effects in copy constructors have well-defined or
> unspecified behavior?
>

It is unspecified whether any side-effect in a copy-ctor will happen by
virtue of the sub-clause you quote. Any time that the compiler is allowed to
elide a copy-ctor the side effects of a call of the copy-ctor will not
happen if the copy-ctor is elided.  That is exactly the purpose of the
sub-clause. From the programmer's view-point it is bad programming to have
an essential side-effect in a copy-ctor.




--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: SG <s.gesemann@gmail.com>
Date: Sat, 6 Feb 2010 09:32:26 CST
Raw View
On 5 Feb., 01:43, Jonathan Mcdougall <jonathanmcdoug...@gmail.com>
wrote:
> 12.8/15 says: "When certain criteria are met, an implementation is
> allowed to omit the copy construction of a class object, even if the
> copy constructor and/or destructor for the object have side effects."
>
> For an example such as:
>
>  class A
>  {
>  public:
>    A() {}
>    A(const A&) {++s;}
>    static int s;
>  };
>
>  int A::s(0);
>
>  A f()
>  {
>    A a;
>    return a;
>  }
>
>  int main()
>  {
>    A a = f();
>    std::cout << a.s;
>  }
>
> A quick test in Visual C++ 2008 shows "0" with optimizations enabled.
> What is the output considered: Is it well-defined or unspecified?

It's implementation-defined. With a conforming C++ implementation
anything between 0 and 2 (inclusive) is possible, I think. Your
compile seems to have elided two copies:

   1. copy initialization of the return value from local 'a' in f()

   2. copy initialization of 'a' in main from the temporary object
      returned by f()

> Is
> there any mention in the standard (I couldn't find any) that says
> whether side-effects in copy constructors have well-defined or
> unspecified behavior?

I think you misunderstood something. It's not that the standard allows
compilers to ignore the code inside copy ctors. It's that it allows
compilers to REMOVE THE NEED to execute copy ctors and dtors. This is
done by combining source and target object into a single object. In
your case, f's function-local object 'a' actually IS the main
function's local object 'a'. Only a single object is involved that
doesn't require any copying or destruction. If an object of type A is
copied or destroyed the code from its copy ctor resp. dtor is ALWAYS
excecuted.

Cheers,
SG


--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]