c++ - Is std::vector::push_back permitted to throw for any reason other than failed reallocation or construction? -
consider:
std::vector<int> v; v.reserve(1); v.push_back(1); // statement guaranteed not throw?
i've chosen int
because has no constructors throw - if copy constructor of t throws, exception escapes vector<t>::push_back
.
this question applies insert
push_back
, inspired is safe push_back 'dynamically allocated object' vector?, happens ask push_back
.
in c++03 , c++0x standard/fcd, descriptions of vector::insert
if no reallocation happens, iterators/references before insertion point remain valid. don't if no reallocation happens, no exception thrown (unless constructors etc of t).
is there elsewhere in standard guarantee this?
i don't expect push_back
throw in case. gnu implementation doesn't. question whether standard forbids it.
as follow-up, can think of reason why implementation throw? best can think of, if call reserve
ends increasing capacity value in excess of max_size()
, insert
perhaps permitted throw length_error
when max size exceeded. useless increase capacity beyond max_size()
, don't see forbidding that, either [edit: allocator stop increasing capacity beyond max_size
, suggestion might no good.]
well, kind of depends on allocator using.
apart allocator, thing can rely on push_back()
, push_front()
guaranteed noop if exception thrown (23.1-10). standard doesn't forbid push_back()
throwing exceptions.
Comments
Post a Comment