From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 64C5B3858D28; Thu, 26 Jan 2023 10:20:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 64C5B3858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674728459; bh=JpUQA5pwp7WWp2QOHa/nk6zHcOGWG2CNP4Rka1QCamc=; h=From:To:Subject:Date:In-Reply-To:References:From; b=wu2B1v+cdw8MEjTyv8XeXZbQJzhQaGOV10Yu71ct/oTp41qe4Fz6HXK7WVmgPqPiV 9vca1TUG8PlWHkkUDPL1cTUH4/zgq8JZiQlcfUIEI8DDqTgaIX6DFk3g9cUOSRIaWn Xl1kRG/FIdNlRZvFCaksrHEn7sKs2ktsBb/tAYqw= From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug libstdc++/108556] std::sort changes objects' member values Date: Thu, 26 Jan 2023 10:20:58 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: libstdc++ X-Bugzilla-Version: 11.3.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D108556 --- Comment #2 from Jonathan Wakely --- Compiling with -D_GLIBCXX_DEBUG shows the problem: /home/jwakely/gcc/13/include/c++/13.0.1/bits/stl_algo.h:4892: In function: void std::sort(_RAIter, _RAIter, _Compare) [with _RAIter =3D=20 gnu_debug::_Safe_iterator > >, debug::vector, random_access_iterator_tag>;= =20 _Compare =3D main()::] Error: comparison doesn't meet irreflexive requirements, assert(!(a < a)). Objects involved in the operation: instance "functor" @ 0x7ffee875415f { type =3D main::{lambda(Item const&, Item const&)#1}; } iterator::value_type "ordered type" { type =3D Item; } Aborted (core dumped) Your comparison function fails to meet the requirements of a strict weak ordering, so your program has undefined behaviour. https://www.boost.org/sgi/stl/StrictWeakOrdering.html https://en.cppreference.com/w/cpp/named_req/Compare=