Re: Minor modification: nothrow guarantee for clear() in sequence container
On Jul 31, 7:22 am, jcof...@taeus.com (Jerry Coffin) wrote:
In article <1185781837.435107.100...@q75g2000hsh.googlegroups.com>,
james.ka...@gmail.com says...
[ ... ]
A better solution would probably be to specify explicitly in
erase that if the first iterator is begin(), and the second
end(), neither the copy constructor nor the assignment operator
will be called. I don't normally like such special cases, but
in this case, I suspect that there is some justification for it.
I think the specification could be made more general: that the copy
constructor/assignment operator is called no more than end()-last times
(where 'last' is the iterator specifying the end of the range to be
erased).
This does constrain the implementation. It prevents the data block from
being shrunk when/if you erase enough to drop the in-use section below a
specific threshold. Then again, while (I believe) that's currently
possible, it's certainly not required, so existing code shouldn't depend
on it anyway.
An implementation is not currently allowed to shrink the block
anyway, since that might invalidate iterators. Note that the
iterator guarantees are very strong:
std::vector< int > v ;
for ( int i = 0 ; i < 10 ; ++ i ) {
v.push_back( i ) ;
}
assert( v.capacity() >= 10 ) ;
v.clear() ;
v.push_back( 0 ) ;
v.push_back( 1 ) ;
std::vector< int >::iterator i = v.begin() + 1 ;
for ( int i = 2 ; i < 10 ; ++ i ) {
v.push_back( i ) ;
}
// here, iterator i is guaranteed to be valid,
// since we have not exceeded the previously
// known capacity.
--
James Kanze (GABI Software) email:james.kanze@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
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]