BDE 4.14.0 Production release
|
Macros | |
#define | bslma_DestructorGuard bslma::DestructorGuard |
This alias is defined for backward compatibility. | |
Provide a guard to unconditionally manage an object.
This component provides a guard class template, bslma::DestructorGuard
, to unconditionally manage an (otherwise-unmanaged) object of parameterized TYPE
supplied at construction. The managed object is destroyed automatically when the guard object goes out of scope by calling the (managed) object's destructor.
Suppose we have a situation where one of the two constructors will be called to create an object on the stack for performance reasons. The construction thus occurs within either of the branches of an if
statement, so the object itself, to survive the end of the "then" or "else" block, must be constructed in a bsls::ObjectBuffer
. Once constructed, the object would not be destroyed automatically, so to make sure it will be destroyed, we place it under the management of a bslma::DestructorGuard
. After that, we know that however the routine exits – either by a return or as a result of an exception being thrown – the object will be destroyed.
Note that regardless of how this routine terminates, myVec
will be destroyed.
Note that push_back
could allocate memory and therefore may throw. However, if it does, myVec
will be destroyed automatically along with guard
.
Note that myVec
is automatically destroyed as the function returns.
Note that myVec
is destroyed after the temporary containing the return value is created.
#define bslma_DestructorGuard bslma::DestructorGuard |