Consider following snippet:
#include <bits/stdc++.h>
using namespace std;
int main() {
int x=3;
int y=1;
int z=2;
cout<<(&x)<<' '<<(&y)<<' '<<(&z)<<endl;
set<reference_wrapper<int>> a;
a.insert(ref(x));
a.insert(ref(y));
a.insert(ref(z));
for(const auto& i: a) cout<<i<<' '<<endl;
y=10;
for(const auto& i: a) cout<<i<<' ';
return 0;
}
What would happen to underlying container when a property used by container for sorting is modified?
Edit1 : Given the answer I posted that ordering can go wrong, is it a bad practice to use reference_wrapper inside stl containers then (specially where mutations can cause container to invalidate its assertions)?
From what I can try by running code, ordering goes wrong but since it was holding a reference, value is correctly updated. So one should be careful when using reference_wrapper inside containers