Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

std::unique_copy with overlapping ranges

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading