On 3 avr, 16:14, Jerry Coffin <jcof...@taeus.comwrote:
In article <ft2ck5$2j...@ikaria.belnet.be>, no_valid_em...@spam.com
says...
If you use the swap function of an std::vector, what does it do?
Does it swap each element separately by means of copying to a tmp
variable, or does it just swap some pointers to memory blocks.
It's pretty much required to do the latter. It's required to have
constant complexity, and it's can't throw an exception (a swap() can
only throw an exception if the container's Compare() function throws the
exception, and a vector doesn't have a Compare() function).
I've been wondering about this. What about the allocators? If
I understand the philosophy behind them, all memory that was
allocated must be freed by an allocator of the same type, which
compares equal to the one used to allocate. The same type is
automatic, since the type of the allocator is part of the type
of the template specialization. But maintaining the second
condition means somehow swapping the allocators as well, at
least if they don't compare equal. And off hand, I don't see
any requirement that allocators are "Swappable", nor that they
cannot throw if you attempt to swap them with std::swap.
Is this an oversight in the standard, or is there something I've
missed?
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34