Date
2012-11-02.17:33:46
Message id
5886

Content

The output of the program below should be:

"three two one null \n"

But when std::reverse_copy is implemented as described in N3291 [alg.reverse] it's:

"null three two one \n"

because there's an off by one error in [alg.reverse]/4; the definition should read:

*(result + (last - first) - 1 - i) = *(first + i)

Test program:

#include <algorithm>
#include <iostream>

template <typename BiIterator, typename OutIterator>
auto
reverse_copy_as_described_in_N3291(
  BiIterator first, BiIterator last, OutIterator result )
-> OutIterator
{
  // 25.3.10/4 [alg.reverse]:
  // "...such that for any non-negative integer i < (last - first)..."
  for ( unsigned i = 0; i < ( last - first ); ++i )
    // "...the following assignment takes place:"
    *(result + (last - first) - i) = *(first + i);

  // 25.3.10/6
  return result + (last - first);
}

int main()
{
  using std::begin;
  using std::end;
  using std::cout;

  static const char*const in[3]  { "one", "two", "three" };
  const char*             out[4] { "null", "null", "null", "null" };

  reverse_copy_as_described_in_N3291( begin( in ), end( in ), out );

  for ( auto s : out )
    cout << s << ' ';

  cout << std::endl;

  return 0;
}