Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-08-30 09:45:47


Howard Hinnant wrote:
> On Saturday, August 30, 2003, at 9:31 AM, Peter Dimov wrote:
>
>> The stateful allocator problem is a thorny issue too, since to allow
>> stateful allocators, one needs to specify whether the stored
>> allocator is part of the logical state of the object. IE
>>
>> vector<T, A> v1(a1);
>> vector<T, A> v2(a2);
>>
>> v1 = v2; // which allocator does v1 hold now?
>
> Just to throw in my .02:
>
> v1 holds the same allocator as it was constructed with. Rationale:
> Allocator state should stay with the memory it allocates so that it
> can deallocate it when the time comes (much like the shared_ptr
> philosophy). Since vector assignment does not transfer ownership of
> memory, it should not assign allocators.
>
> But consider:
>
> swap(v1, v2);
>
> Here memory ownership is swapped between the vectors. Therefore,
> imho, allocator state should also be swapped.

This is all very reasonable; the allocator is not part of the logical state
(merely an implementation detail) and therefore v1 and v2 can be equivalent
(Assignable postcondition) even though they have different allocators.

If I want to assign the allocator I'll have to use

vector<T, A>(v2).swap(v1);

On the other hand, the alternative seems reasonable, too. The allocator is
part of the state (accessor is available), therefore after v1 = v2 v1 holds
a2. If I want to retain the allocator I can use

v1.assign(v2.begin(), v2.end());

which works for any v2, not just the specific vector<T, A>.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk