public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-4873] libstdc++: Fix regression in std::list::sort [PR66742] Date: Wed, 3 Nov 2021 16:51:28 +0000 (GMT) [thread overview] Message-ID: <20211103165128.B808E385801F@sourceware.org> (raw) https://gcc.gnu.org/g:1e7a269856fd67aff78ac874bec96d31a54b2fd9 commit r12-4873-g1e7a269856fd67aff78ac874bec96d31a54b2fd9 Author: Jonathan Wakely <jwakely@redhat.com> Date: Tue Nov 2 10:21:01 2021 +0000 libstdc++: Fix regression in std::list::sort [PR66742] The standard does not require const-correct comparisons in list::sort. libstdc++-v3/ChangeLog: PR libstdc++/66742 * include/bits/list.tcc (list::sort): Use mutable iterators for comparisons. * include/bits/stl_list.h (_Scratch_list::_Ptr_cmp): Likewise. * testsuite/23_containers/list/operations/66742.cc: Check non-const comparisons. Diff: --- libstdc++-v3/include/bits/list.tcc | 4 ++-- libstdc++-v3/include/bits/stl_list.h | 8 ++++---- .../23_containers/list/operations/66742.cc | 23 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/libstdc++-v3/include/bits/list.tcc b/libstdc++-v3/include/bits/list.tcc index 7f4e1569ab1..cc750c98a2d 100644 --- a/libstdc++-v3/include/bits/list.tcc +++ b/libstdc++-v3/include/bits/list.tcc @@ -499,7 +499,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _Scratch_list* __fill = __tmp; _Scratch_list* __counter; - _Scratch_list::_Ptr_cmp<const_iterator, void> __ptr_comp; + _Scratch_list::_Ptr_cmp<iterator, void> __ptr_comp; __try { @@ -623,7 +623,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER _Scratch_list* __fill = __tmp; _Scratch_list* __counter; - _Scratch_list::_Ptr_cmp<const_iterator, _StrictWeakOrdering> __ptr_comp + _Scratch_list::_Ptr_cmp<iterator, _StrictWeakOrdering> __ptr_comp = { __comp }; __try diff --git a/libstdc++-v3/include/bits/stl_list.h b/libstdc++-v3/include/bits/stl_list.h index 96d2a2f0f69..ffaaa6dfd97 100644 --- a/libstdc++-v3/include/bits/stl_list.h +++ b/libstdc++-v3/include/bits/stl_list.h @@ -174,8 +174,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Cmp _M_cmp; bool - operator()(const __detail::_List_node_base* __lhs, - const __detail::_List_node_base* __rhs) /* not const */ + operator()(__detail::_List_node_base* __lhs, + __detail::_List_node_base* __rhs) /* not const */ { return _M_cmp(*_Iter(__lhs), *_Iter(__rhs)); } }; @@ -183,8 +183,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION struct _Ptr_cmp<_Iter, void> { bool - operator()(const __detail::_List_node_base* __lhs, - const __detail::_List_node_base* __rhs) const + operator()(__detail::_List_node_base* __lhs, + __detail::_List_node_base* __rhs) const { return *_Iter(__lhs) < *_Iter(__rhs); } }; diff --git a/libstdc++-v3/testsuite/23_containers/list/operations/66742.cc b/libstdc++-v3/testsuite/23_containers/list/operations/66742.cc index 24bda3920d8..94a37a31de7 100644 --- a/libstdc++-v3/testsuite/23_containers/list/operations/66742.cc +++ b/libstdc++-v3/testsuite/23_containers/list/operations/66742.cc @@ -48,8 +48,31 @@ test01() VERIFY( is_sorted(l, std::greater<int>()) ); } +void +test02() +{ + // The standard doesn't require comparisons to be const-correct. + // The initial fix for PR 66742 caused a regression here. + + struct X + { + bool operator<(X&) /* non-const */ { return false; } + }; + + struct Cmp + { + bool operator()(X&, X&) /* non-const */ { return false; } + }; + + std::list<X> l; + l.sort(); + Cmp c; + l.sort(c); +} + int main() { test01(); + test02(); }
reply other threads:[~2021-11-03 16:51 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20211103165128.B808E385801F@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).