Settings and maps keep items in order according to the sorting condition. In order for the user code not to interrupt the invariants, the key cards and the entire element in the set must be constant. Your problem is that the saved item is not a SmallObject , but a const SmallObject .
If this was not limited, you could:
int init[] = { 1, 2, 3, 4, 5 }; std::set<int> values( init, init+5 ); std::copy( values.begin(), values.end(), std::ostream_iterator<int>(std::cout, " ")); // 1 2 3 4 5 *(values.find(3)) = 5; // luckily this does not work! std::copy( values.begin(), values.end(), std::ostream_iterator<int>(std::cout, " ")); // 1 2 5 4 5 -- not in order!!!
The problem is not only that now the set element would not be in order, but depending on how the tree was built, there may be elements that are present in the set but cannot be found.
source share