Skip to content

Instantly share code, notes, and snippets.

@fxb
Created January 11, 2016 14:59
Show Gist options
  • Save fxb/335f0d61b48ebd9df160 to your computer and use it in GitHub Desktop.
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).
#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;
}
@fxb
Copy link
Author

fxb commented Jan 11, 2016

@fxb
Copy link
Author

fxb commented Jan 11, 2016

@fxb
Copy link
Author

fxb commented Jan 11, 2016

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