33 lines
690 B
Plaintext
33 lines
690 B
Plaintext
Contrary to possible developer expectations, a template constructor will not suppress the compiler-generated copy constructor. This may lead to incorrect copy semantics for members requiring deep copies.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
class A
|
|
{
|
|
public:
|
|
A ( );
|
|
// A ( A const & rhs ); Example 1 - implicitly generated
|
|
template <typename T>
|
|
A ( T const & rhs ) // Example 2
|
|
: i ( new int32_t )
|
|
{
|
|
*i = *rhs.i;
|
|
}
|
|
private:
|
|
int32_t * i; // Member requires deep copy
|
|
};
|
|
|
|
void f ( A const & a1 )
|
|
{
|
|
A a2 ( a1 ); // Noncompliant, unexpectedly uses Example 1, which will result in a shallow copy of 'i', instead of a deep copy
|
|
}
|
|
----
|
|
|
|
|
|
== See
|
|
|
|
* MISRA {cpp}:2008, 14-5-2
|
|
|