http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52822
Bug #: 52822
Summary: [C++11] stable_partition destroys sequence due to
inappropriate self-move-assignment
Classification: Unclassified
Product: gcc
Version: 4.8.0
Status: UNCONFIRMED
Severity: major
Priority: P3
Component: libstdc++
AssignedTo: [email protected]
ReportedBy: [email protected]
The following test program should print "1" twice, since stable_partition is
only supposed to rearrange values, not modify them.
$ cat test.cc
#include <vector>
#include <utility>
#include <algorithm>
#include <iostream>
using namespace std;
bool pred(const vector<int>&) { return true; }
int main() {
vector<vector<int> > v(1);
v[0].push_back(7);
cout << v[0].size() << '\n';
stable_partition(v.begin(), v.end(), pred);
cout << v[0].size() << '\n';
}
$ g++-4.6pre --version
g++-4.6pre (GCC) 4.6.4 20120330 (prerelease)
$ g++-4.6pre -std=c++98 -g3 test.cc -o test && ./test
1
1
$ g++-4.6pre -std=c++0x -g3 test.cc -o test && ./test
1
0
$ g++-4.7pre --version
g++-4.7pre (GCC) 4.7.1 20120330 (prerelease)
$ g++-4.7pre -std=c++0x -g3 test.cc -o test && ./test
1
0
$ g++-4.8pre --version
g++-4.8pre (GCC) 4.8.0 20120330 (experimental)
$ g++-4.8pre -std=c++0x -g3 test.cc -o test && ./test
1
0
I believe this happens because
http://gcc.gnu.org/viewcvs/trunk/libstdc%2B%2B-v3/include/bits/stl_algo.h?revision=184997&view=markup#l1827
move-assigns *__result1 from *__first even when those are the same object,
which violates
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#1204.
I haven't audited the rest of the library to look for more instances of this
mistake. I'm planning to switch the default compilation mode to c++0x and run
the gcc-4.6 test suite to look for more problems, but I'm not currently
planning to do the same for 4.7 or trunk.