| High Integrity CPP Guideline 17.21 | Minimise use of the Standard Template Library 'auto_ptr'. |
| Justification |
'auto_ptr' has destructive copy semantics which may be non-intuitive and can lead to erroneous usage.
void foo( auto_ptr<int> ai );
void bar()
{
auto_ptr<int> ai( new int );
foo( ai ); // destructive copy
*ai = 10; // error ai no longer exists
}
|
| Exception |
If you use 'auto_ptr' take note of the following: - Do not use auto_ptr with array types. - Only use auto_ptr with dynamically allocated variables. - Be aware that implicit conversions can take place between auto_ptrs of different types where a conversion exists for the underlying pointer types. - Use auto_ptr where "ownership-transfer semantics" are required, for example do not use auto_ptr where you need two pointers to the same object concurrently. |