I am reading Exceptional C++ (Herb Sutter) and there is something that I don't get at all concerning vector insertion. On page 59 it says:
1) "multi-elements inserts ("iterator range" inserts) are never strongly exception-safe"
2) "for vector<T>.... inserts and erases (whether single- or multi-elements) are strongly exception-save as long as T's copy constructor and assignment do not throw"
It then goes on explaining that those limitation are required "because to roll-back either kind of operation isn't possible without extra space/time overhead".

What I don't get is this: in my (humble) view it is easy to implement the vector so that there is only a time overhead (and no space overhead) and furthermore that time overhead only occurs when an exception actually occurs. (the way to do appears to be by he swap technique described in the previous items in the book).

So my question is: does the standard has this eminently stringent limitation because:
1) the implementation I think would work, doesn't actually work (see below) confirming my status of a newbie and because there does can't exist any such implementation for theoretically proven reasons?
2) because the time cost of repairing the exception is deemed too expensive (even if it occurs by definition almost never!) than those limitations that are quite severe (and always occurs)?

Implementation note:
My implementation for vector range insertion, even permitting exceptions during assignment:
Start with vector V containing for example 20 elements and you want to insert at position 10, 5 more elements.
If V capacity has enough to accomodate the 5 elements{
- copy elements from 20 to 10 to position i + 5 while trapping exception: if exception while copyint n'th element, copy back to original position (this is the only time-cost added), exit levaing V similar to original
- copy insert the 5 elements while trapping exceptions: if exception occurs, destroy already copied elements then recopy back element 11 to 20 to original position. Your initial V is intact. exit.
- create a new vector W and copy 10 element, then the new 5 then the last 10 while trapping exceptions: if exceptions occurs, destroy W and exit, leaving you with the original V intact
- if no exception swap pointers of V and W then destroy W.

5 Years
Discussion Span
Last Post by trantran

Forget that post!
I think the answer is simpy that the copying back (in the in-place first part) if the exception occurs could itself throw an exception, breaking the implementation

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.