|
|
|
|
|
by tialaramex
814 days ago
|
|
> Consider lower_bound. The behaviour is undefined if the input is not sorted. That's just the way it is. No, it's a classic WG21 choice in that it's pointlessly worse, that's not "the way it is" it's just the way WG21 decided to define it in C++. More reasonable choices would obviously include the choice made over in std::set where lower_bound gives you a past-the-end iterator if there isn't an apparent answer for your inputs. Undefined Behaviour is the worst choice, which is why in a horrible way it makes sense as WG21's default - a group known for always choosing bad defaults. > You can't magically make a program well-formed if you breach preconditions Of course you can, it's not even hard. |
|
std::set preserves sorted as an invariant, so there is no such precondition on its lower_bound member.
The important requirement for std::set is that the comparison function satisfies a strict weak ordering. Again, the behaviour is undefined if it does not.
There is no substitute for correctness. You need to satisfy the requirements or stuff doesn't work. Whether the "doesn't work" means an error, a crash, corrupt memory, resource leaks or incorrect results doesn't really make much of a difference: the code is incorrect and should be fixed.