Hi Martin,

ISO 14882:2003 (the C++03) standard doesn't clearly specify how
containers must swap its contents. It's clearly specified in C++0x
drafts. Most implementations simply exchange internal block (pointers)
and capacity, if allocators are the same. Otherwise, each element will
be copied.

ref_ptr can be easily used in either case (latter case being the
inefficient/different allocator case), because it satisfies the
requirements (from C++03):
25.2.2 Swap [lib.alg.swap]
template<class T> void swap(T& a, T& b);
1 Requires: Type T is CopyConstructible (20.1.3) and Assignable (23.1).
2 Effects: Exchanges values stored in two locations.

ref_ptr<T> is CopyConstructible and Assignable; its copies are equivalent.

Ismail

On Wed, Apr 15, 2009 at 5:10 PM, Martin Beckett <m...@mgbeckett.com> wrote:
> vector typically doubles the size of memory it reserves when it needs more 
> space. This is because every time it allocates new memory it must move the 
> contents of the existing memory - it's slow to copy all the existing elements 
> and so it makes sense to only increase as few times as possible (see 
> http://www.ddj.com/cpp/184401375 for a discussion).
> Unfortunately this isn't a good stratergy when you are almost out of memory.
>
> Another problem is that the memory must be contiguous (ie in one block), so 
> if you frequently allocate and delete memory there might not be a single free 
> block large enough - even if you have enough overall memory.
>
> There is also a little 'trick' to reduce a vector back down to the minimum 
> required memory - see http://www.gotw.ca/gotw/054.htm
>
> Code:
> vector<stuff>( c ).swap( c );
>
>
>
> (note - can someone confirm the swap() trick works with ref counting?)
>
> ------------------
> Read this topic online here:
> http://forum.openscenegraph.org/viewtopic.php?p=10259#10259
>
>
>
>
>
> _______________________________________________
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to