Does std::copy_n work with overlapping ranges?

Viewed 547

I was looking in the C++ standard at N3485 25.3.1 [alg.copy], which defines 4 algorithms:

  • copy
  • copy_backward
  • copy_if
  • copy_n

In the description for copy, there is this note 25.3.1 [alg.copy]/3:

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

That is, copy does not always work correctly when the ranges overlap (similar to memcpy).

copy_backward and copy_if have similar language prohibiting overlapping ranges (25.3.1 [alg.copy]/14 and 25.3.1 [alg.copy]/8, respectively).

However, there is no such prohibition for copy_n, and there is no copy_n_backward. Does this mean that copy_n does the right thing when the ranges overlap?

(MSVC++'s implementation of copy_n appears to delegate to std::memmove, so I know it is safe here on MSVC++ 2013. But I don't want to rely on this if the standard implies otherwise)

2 Answers
Related