Next: High Integrity CPP Rule 17.9  Up: 17 Standard Template Library (STL)  Previous: High Integrity CPP Guideline 17.7  Contents

High Integrity CPP Rule 17.8   Never create containers of auto_ptrs.

Justification

'auto_ptr' has destructive copy semantics, this means that when you copy an auto_ptr the source loses its value. STL containers require that element types provide copy semantics such that the source and destination are equivalent after a copy.

The C++ Standard prohibits containers of auto_ptrs so they should not compile. However some STL implementations and some compilers do not reject them.

   
   class MyClass {};
   
   void foo( vector< auto_ptr< MyClass > >& myVec )
   {
      // After myObj2 is initialised myObj1 is a 0 ptr!
      //
      auto_ptr< MyClass > myObj1 = myVec[ 0 ];
      auto_ptr< MyClass > myObj2 = myObj1; 
   }
Reference

Effective STL Item 8;


HICPP VERSION 2.4  http://www.codingstandard.com   Copyright: © 2007 THE PROGRAMMING RESEARCH GROUP