Home > front end >  std::unique_copy with overlapping ranges
std::unique_copy with overlapping ranges

Time:04-15

template< class InputIt, class OutputIt >
OutputIt unique_copy( InputIt first, InputIt last,
                      OutputIt d_first );

Is it valid to use std::unique_copy if input range and output range overlap? Consider the following two example cases

auto d_last = std::unique_copy(first, last, d_first);

  1. d_first <= first <= d_last <= last
  2. first <= d_first <= last <= d_last

CodePudding user response:

The preconditions for std::unique_copy are described in [algorithms#alg.unique-8]:

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

Preconditions:

  • The ranges [first, last) and [result, result (last-first)) do not overlap.

So this is undefined behavior.

  • Related