Start here

Home
About Klocwork
What's new
Fixed issues
Release notes
Installation

Reference

C/C++ checkers
Java checkers
C# checkers
MISRA C 2004 checkers
MISRA C++ 2008 checkers
MISRA C 2012 checkers
MISRA C 2012 checkers with Amendment 1
Commands
Metrics
Troubleshooting
Reference

Product components

C/C++ Integration build analysis
Java Integration build analysis
Desktop analysis
Refactoring
Klocwork Static Code Analysis
Klocwork Code Review
Structure101
Tuning
Custom checkers

Coding environments

Visual Studio
Eclipse for C/C++
Eclipse for Java
IntelliJ IDEA
Other

Administration

Project configuration
Build configuration
Administration
Analysis performance
Server performance
Security/permissions
Licensing
Klocwork Static Code Analysis Web API
Klocwork Code Review Web API

Community

View help online
Visit RogueWave.com
Klocwork Support
Rogue Wave Videos

Legal

Legal information

MISRA.COPYASSIGN.TMPL

Class has a template assignment operator with a single generic parameter, but has no explicit copy assignment operator defined.

MISRA-C++ Rule 14-5-3 (required): A copy assignment operator shall be declared when there is a template assignment operator with a parameter that is a generic parameter.

Rationale

Contrary to possible developer expectations, a template assignment operator will not suppress the compiler generated copy assignment operator. This may lead to incorrect copy semantics for members requiring deep copies.

Example

class A
{
public:
   // A & operator= ( A const & rhs ) Example 1 - implicitly generated
   // {
   // i = rhs.i;
   // return *this;
   // }

   template <typename T>
   T & operator= ( T const & rhs ) // Example 2
   {
      if ( this != &rhs ) {
         delete i;
         i = new int32_t;
         *i = *rhs.i;
      }
      return *this;
   }
private:
   int32_t * i; // Member requires deep copy
};

void f ( A const & a1, A & a2 )
{
   a2 = a1; // Unexpectedly uses Example 1
}

The implicitly generated copy assignment operator Example 1 will be used to copy 'a1' to 'a2'. Therefore, a shallow copy on the pointer member 'i' will result in both 'a1.i' and 'a2.i' pointing to the same object. Was this the intent, or was it expected that a new object would be created and initialized?