#include <stlsoft/memory/auto_destructor.hpp>
T | The value type |
{ stlsoft::auto_destructor<MyClass> adi(new MyClass(1, 2)); . . . } // The MyClass instance is deleted here
The horrendous, and widely lamented, problems with std::auto_ptr
are avoided by proscribing copy operations. Hence:
stlsoft::auto_destructor<MyClass> adi(new MyClass(1, 2)); stlsoft::auto_destructor<MyClass> adi2(adi); // Compile error!
But resources can be returned out of auto_destructor instances from functions by virtue of return_value_destructor, as in:
stlsoft::return_value_destructor<MyClass> f(int i, int j) { stlsoft::auto_destructor<MyClass> adi(new MyClass(i, j)); . . . } // The MyClass instance is deleted here stlsoft::auto_destructor<MyClass> adi2 = f(1, 2);
In this way, the resource is completely safely managed, without there being any unfortunate side-effects (as is the case with std::auto_ptr
).
Types | |
typedef T | value_type |
The value type. | |
typedef auto_destructor< T > | class_type |
The current parameterisation of the type. | |
typedef return_value_destructor< T > | return_value_type |
The return value type. | |
Public Member Functions | |
Construction | |
auto_destructor (value_type *t) | |
Constructs from the pointer to an instance whose lifetime will be managed. | |
auto_destructor (proxy_type rhs) | |
Proxy move constructor. | |
~auto_destructor () throw () | |
Destroys the managed instance. | |
Operations | |
value_type * | detach () |
Detaches the managed instance from the auto_destructor and returns a pointer to it to the caller. | |
Accessors | |
value_type * | operator-> () |
Implicit conversion to pointer to the managed instance. | |
value_type const * | operator-> () const |
Implicit conversion to pointer-to-const to the managed instance. | |
value_type * | get_ptr () const |
Returns the pointer. | |
value_type * | get () const |
Returns the pointer. |
typedef T value_type |
The value type.
typedef auto_destructor<T> class_type |
The current parameterisation of the type.
typedef return_value_destructor<T> return_value_type |
The return value type.
auto_destructor | ( | value_type * | t | ) | [inline, explicit] |
Constructs from the pointer to an instance whose lifetime will be managed.
auto_destructor | ( | proxy_type | rhs | ) | [inline] |
Proxy move constructor.
~auto_destructor | ( | ) | throw () [inline] |
Destroys the managed instance.
value_type* detach | ( | ) | [inline] |
Detaches the managed instance from the auto_destructor and returns a pointer to it to the caller.
value_type* operator-> | ( | ) | [inline] |
Implicit conversion to pointer to the managed instance.
value_type const* operator-> | ( | ) | const [inline] |
Implicit conversion to pointer-to-const to the managed instance.
value_type* get_ptr | ( | ) | const [inline] |
Returns the pointer.
value_type* get | ( | ) | const [inline] |