Skip to content

Instantly share code, notes, and snippets.

@Kiterai
Last active May 14, 2020 17:00
Show Gist options
  • Save Kiterai/9a1b64bfefadb11ba561876e75e65d11 to your computer and use it in GitHub Desktop.
Save Kiterai/9a1b64bfefadb11ba561876e75e65d11 to your computer and use it in GitHub Desktop.
64bitのUbuntuでppa:ubuntu-toolchain-r/testリポジトリからインストールしたGCC9.xで-O2にするとstd::sortの処理結果がおかしくなる
#include <iostream>
#include <algorithm>
using namespace std;
struct A
{
long long a, b;
};
const int len = 10;
int main()
{
A arr[len];
for (int i = 0; i < len; i++) {
arr[i] = { rand() % 5, rand() % 5 };
}
auto m = &A::b;
// expects sort by A::b
sort(arr, arr + len,
[m](A a, A b) {
return a.*m < b.*m;
});
for (int i = 0; i < len; i++) {
cout << arr[i].a << ' ' << arr[i].b << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment