Is uninitialized_copy() exception-safe?
-
26-06-2021 - |
Domanda
MSDN and other places say that uninitialized_copy
provides a strong exception guarantee, but other C++ references don't.
Is this in fact guaranteed by C++, or not?
Soluzione
Yes, C++03 does provide this guarantee, but it's worth double-checking for your implementations.
From a draft copy I had on my machine, 20.4.4:
All the iterators that are used as formal template parameters in the following algorithms are required to have their
operator*
return an object for which operator& is defined and returns a pointer toT
.
In the algorithmuninitialized_copy
, the formal template parameterInputIterator
is required to satisfy the requirements of an input iterator (24.1.1).
In all of the following algorithms, the formal template parameterForwardIterator
is required to satisfy the requirements of a forward iterator (24.1.3) and also to satisfy the requirements of a mutable iterator (24.1), and is required to have the property that no exceptions are thrown from increment, assignment, comparison, or dereference of valid iterators.
In the following algorithms, if an exception is thrown there are no effects.
uninitialized_copy
(etc.)
So yes, that means the "possible implementation" you see on some pages can be incorrect.