== Why is this an issue?

If a compiler implementation detects that a call to a copy constructor is redundant, then it is permitted to omit that call, even if the copy constructor has a side effect other than the construction of a copy of the object. This is called "copy elision".

It is therefore important to ensure that a copy constructor does not modify the program state, since the number of such modifications may be indeterminate.


=== Noncompliant code example

[source,cpp]
----
class A
{
public:
  A ( A const & rhs )
  : m_i ( rhs.m_i )
  {
    ++m_static; // Non-compliant, might not be executed
  }
private:
  int32_t m_i;
  static int32_t m_static;
};
----


== Resources

* MISRA {cpp}:2008, 12-8-1
* https://wiki.sei.cmu.edu/confluence/x/gXs-BQ[CERT, OOP58-CPP.] - Copy operations must not mutate the source object


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

This code within a copy constructor might not be executed and should be removed.


'''
== Comments And Links
(visible only on this page)

=== on 14 Oct 2014, 17:49:57 Ann Campbell wrote:
\[~samuel.mercier] please correct the following

* There is no message
* Add a See section to the description listing the appropriate MISRA number
* Fill in Applicability.
* Make sure the appropriate MISRA C and MISRA {cpp} fields on the references tab are filled in


=== on 17 Oct 2014, 14:38:02 Ann Campbell wrote:
\[~samuel.mercier] I'm tempted to tag this with 'bug'. WDYT?

endif::env-github,rspecator-view[]
