https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124051

            Bug ID: 124051
           Summary: forward_list::sort is not exception-safe
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ensadc at mailnesia dot com
  Target Milestone: ---

https://godbolt.org/z/aGPnK41Kr
====
#include <forward_list>

int main() {
    std::forward_list<int> mylist{5, 4, 3, 2};
    try {
        int i = 0;
        mylist.sort([&](int a, int b) {
            if (++i == 2) throw 42;
            return a < b;
        });
    } catch (...) {} // crash
}
====
[forward.list.ops]/29 says "Effects: Sorts the list according to the operator<
or the comp function object. If an exception is thrown, the order of the
elements in *this is unspecified. Does not affect the validity of iterators and
references."

This seems to suggest that `forward_list::sort` should provide basic exception
guarantee, instead of breaking the class invariants when the comparator throws.

Reply via email to