Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ STL's copy() exception safety

Tags:

c++

exception

stl

If I do

std::copy(source, source + size, destination);

Do I have strong exception safety guarantee? I.e. if std::copy throws, destination is left unchanged?

like image 691
kirillbobyrev Avatar asked Mar 12 '15 17:03

kirillbobyrev


1 Answers

From the standard:

25.3.1 Copy [alg.copy]

template<class InputIterator, class OutputIterator>
   OutputIterator copy(InputIterator first, InputIterator last,
                       OutputIterator result);

1 Effects: Copies elements in the range [first,last) into the range [result,result + (last -first)) starting from first and proceeding to last. For each non-negative integer n < (last -first), performs *(result + n) = *(first + n).

2 Returns: result + (last - first).

3 Requires: result shall not be in the range [first,last).

4 Complexity: Exactly last - first assignments.

It does not make any guarantees about exception safety. It also does not specify what the behavior would be if result is in the range [first,last).

like image 179
R Sahu Avatar answered Nov 15 '22 22:11

R Sahu