Re: smart pointer clarifications
On Aug 18, 7:28 pm, "Phil Bouchard" <p...@fornux.com> wrote:
I am currently writting a smart pointer which is reasonnably stable and I
decided supporting allocators for completion because of its increase in
efficiency when the same pool used by containers is shared. I was told=
the
new standards are being finalized and I am hoping minor but important
changes could be applied before anything else.
To give a quick overview on the current status of this topic, you will fi=
nd
below the latest text relating to allocator member functions (n2641). =
We
see over there there are no dictinction between the pointer type returned=
by
allocate() and the pointer type expected by deallocate() and destroy().
This is a big problem because this logic doesn't make sense anymore if sm=
art
pointers are to be used. Smart pointers are owners of their objects an=
d
aren't in no way going to give away external access to its object. We =
need
these functions to use
1) distinct pointer type from the one returned by allocate()
2) make the parameter (pointer) passed as a non-const reference
The latter is necessary to make changes to either the smart pointer itsel=
f
or the object pointed to in case raw pointer are used. We can see my
personnal implementation here and I would like to propose the following
function signatures:
template <typename T>
class shifted_allocator
{
public:
typedef shifted<T> value_type;
typedef shifted_ptr<T> pointer;
typedef shifted_ptr<const T> const_pointer;
value_type * allocate(size_type s, const void * = 0);
void deallocate(pointer & p, size_type);
void construct(value_type * p, const T & x);
void destroy(pointer & p);
...
};
[snip]
Perhaps I'm missing something. Smart pointers generally take over
ownership AFTER allocation, e.g.:
tr1::shared_ptr<int> spi1( new int(42) );
tr1::shared_ptr<int> spi2( FactoryFunction() );
A shared_ptr's deleter can tell it how to do some special release
procedure, e.g.:
tr1::shared_ptr<FILE> file( fopen( "some.txt", "r" ), &fclose );
Why do allocators need to become aware of smart pointers?
Cheers! --M