Created
January 11, 2016 14:59
-
-
Save fxb/335f0d61b48ebd9df160 to your computer and use it in GitHub Desktop.
std::random_shuffle / std::shuffle swap element with itself (move element to itself).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <algorithm> | |
#include <cassert> | |
#include <vector> | |
#include <random> | |
using namespace std; | |
struct Foo { | |
public: | |
Foo() {} | |
Foo &operator=(Foo &&other) { | |
assert(&other != this); | |
return *this; | |
}; | |
Foo(Foo &&other) { | |
assert(&other != this); | |
} | |
}; | |
int main() { | |
vector<Foo> vec(1337); | |
random_shuffle(vec.begin(), vec.end()); | |
//mt19937 g; | |
//shuffle(vec.begin(), vec.end(), g); | |
return 0; | |
} |
Related libstdc++ bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59603
libc++ seems to protect against it, in at least two of three cases: https://github.com/llvm-mirror/libcxx/blob/master/include/algorithm#L3079
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use http://webcompiler.cloudapp.net/ to run.