copy() algorithm in the C++ Standard Template Library (STL)
Luke Chang
Luke.Chang@calix.com
Tue May 7 18:43:00 GMT 2002
The copy() algorithm in the C++ STL takes on the following implementation:
template <class InputIterator, class OutputIterator>
OutputIterator copy(InputIterator first, InputIterator last,
OutputIterator result);
The copy() algorithm should NOT allow result to be in the range [first,
last]. But
g++ version 3.0 violates this rule.
I have a C++ program as follows:
#include <algorithm>
#include <iostream>
using namespace std;
const unsigned short int array_size = 10; // same as but better than
#define
int main() {
int i, ar[array_size];
fill(ar, ar + array_size, 0);
for (i = 0; i < 5; i++) {
ar[i] = i + 1;
}
cout << "Initial ar[]:" << endl;
copy(ar, ar + array_size, ostream_iterator<int>(cout, " "));
cout << endl;
copy(ar, ar + (array_size / 2), ar + 3);
cout << "After first copy:" << endl;
copy(ar, ar + array_size, ostream_iterator<int>(cout, " "));
cout << endl;
return 0;
}
g++ 3.0 compiles this program just fine. If we follow the rule that result
MUST NOT be in the range [first, last], then I expect the output to be:
Initial ar[]:
1 2 3 4 5 0 0 0 0 0
After first copy:
1 2 3 1 2 3 1 2 0 0
Borland produces this output.
But g++ version 3.0 gives the following output:
Initial ar[]:
1 2 3 4 5 0 0 0 0 0
After first copy:
1 2 3 1 2 3 4 5 0 0
I checked several Web sites that discuss the copy() algorithm and all of
them
clearly specify that result MUST NOT be in the range [first, last]. This
means
the output that g++ version 3.0 produces is wrong for my C++ program.
Luke Chang
Lowell, MA
More information about the Gcc-bugs
mailing list