CRITICAL
Rule Definition
The way the copy constructor of 'std::auto_ptr' is implemented makes it incompatible with the collections in the Standard Library.
The copy operation of an 'auto_ptr' is destructive (copying A to B changes the state of A), and does not match the requirements of the standard containers. As a result, 'std::auto_ptr' should not be used in containers/collections.
Remediation
Do not use 'auto_ptr' with collections of the Standard Library. Instead, use collections of 'std::unique_ptr' if your compiler supports C++11, or of a 'shared_ptr' type, such as 'std::shared_ptr', 'std::tr1::shared_ptr' or 'boost::shared_ptr', depending on what you have available.
Violation Code Sample
void f()
{
std::vector<std::auto_ptr<Widget>> v;
}
Fixed Code Sample
void f()
{
std::vector<std::unique_ptr<Widget>> v;
}
Related Technologies
C++
Technical Criterion
Programming Practices - Unexpected Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.