STL: Copying vectors with different allocators

Started by
15 comments, last by magic_man 15 years, 1 month ago
Quote:Original post by stonemetal
have you tried boxing the int?

somthing like

struct PoolID{
PoolID(int a): Pool(a){}
int Pool;
};

vector<int, myalloc<int, PoolID(5)> > foo;
That way you have a unified type for your template.


I'm a bit lost with this suggestion. How should the myalloc template class look like? I mean if I declare it as:
template<typename T, typename T2> class myalloc {} it won't compile, because it would expect a typename not an instance of an actual class for T2.

If I tried template<typename T, PoolID T2> class myalloc {} that won't work either, because PoolID is not a constant type and therefore can't be resolved as a template parameter, or am I missing the point here?
Advertisement
Quote:Original post by magic_man
Instead of using the assignment operator you may want to define your own function maybe something like the following.

*** Source Snippet Removed ***


That looks merely like a variation of the suggestions already given above by chairthrower and SiCrane, or did I miss something?
Thanks for the reply, anyway.
It is yes but the important part is the lhs.clear() otherwise it would give different behaviour from the standard. This could be used in place of a user called assignment and could be generalised again to work with any container. As far as I can recall there is no work around to using the assignment operator which works correctly.
"You insulted me!" I did not say that in the private message Tom Sloper!
But then wouldn't vector::assign do the exact same thing (clear what is already there?).

Vector should have all kinds of constructors that take a const reference to the allocator, so if you removed the second template parameter from the allocator, this would work:

int main(){	std::vector<int, TrackedAllocator<int> > vec1((TrackedAllocator<int>(Pool1)));	std::vector<int, TrackedAllocator<int> > vec2((TrackedAllocator<int>(Pool2)));	vec1 = vec2;	return 0;}


But in any case, assign seems a wise thing to do for assigning vectors (I don't think I've ever assigned one container to another anyway, so I think it shouldn't require that much extra typing).

Quote:Original post by Luke1410

I'm a bit lost with this suggestion. How should the myalloc template class look like? I mean if I declare it as:
template<typename T, typename T2> class myalloc {} it won't compile, because it would expect a typename not an instance of an actual class for T2.

If I tried template<typename T, PoolID T2> class myalloc {} that won't work either, because PoolID is not a constant type and therefore can't be resolved as a template parameter, or am I missing the point here?


Sorry, I was in the middle of something else at the time I wrote that. you would obviously have to pass the poolID into the constructor of myalloc not as a template parameter.
Thanks for all the answers... I poked a bit deeper into the STL and it is as we suspected initially: Since the standard does not directly require that containers using different allocators are interchangeable, it's not supported by the implementation. There would be one obvious possibility I can think of to add that support:
Add a cpy ctor to any container class, which accepts any kind of allocator and compare the two allocators using the ==-operator to see if the allocators are interchangable.

However, this would have to be done by Dinkumware to be consistent, so we'll stick with the assign-cpy-suggestion provided here.
Quote:Original post by visitor
But then wouldn't vector::assign do the exact same thing (clear what is already there?).

Of course it would, I do not know what I was thinking. Ignore me.
"You insulted me!" I did not say that in the private message Tom Sloper!

This topic is closed to new replies.

Advertisement