This sounds like a duplicate question to Sorting a Vector of a custom class with std::sort() causes a segmentation fault
and all the answers down the chain of referenced links, but I wasn’t able to see the descriptions in any of those answers about why this weak ordering requirement imposed only for custom types and not for the built pod types.
Could someone please explain why in the following example the v1 vector is sorted just fine and v2 sort crashes the application:
<code>std::vector<char> v1 = {'a','a','a','a','a','g'};
struct P {
char some_char;
};
std::vector<P> v2 = {{'a'},{'a'},{'a'},{'a'},{'a'},{'g'}};
std::sort(v1.begin(), v1.end(), [&](const auto& left, const auto& right){
return left == 'h';
};
std::sort(v2.begin(), v2.end(), [&](const auto& left, const auto& right){
return left.some_char == 'h';
};
</code>
<code>std::vector<char> v1 = {'a','a','a','a','a','g'};
struct P {
char some_char;
};
std::vector<P> v2 = {{'a'},{'a'},{'a'},{'a'},{'a'},{'g'}};
std::sort(v1.begin(), v1.end(), [&](const auto& left, const auto& right){
return left == 'h';
};
std::sort(v2.begin(), v2.end(), [&](const auto& left, const auto& right){
return left.some_char == 'h';
};
</code>
std::vector<char> v1 = {'a','a','a','a','a','g'};
struct P {
char some_char;
};
std::vector<P> v2 = {{'a'},{'a'},{'a'},{'a'},{'a'},{'g'}};
std::sort(v1.begin(), v1.end(), [&](const auto& left, const auto& right){
return left == 'h';
};
std::sort(v2.begin(), v2.end(), [&](const auto& left, const auto& right){
return left.some_char == 'h';
};
3