From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: Re: [committed 2/2] libstdc++: Add [[nodiscard]] to sequence containers
Date: Thu, 5 Aug 2021 15:38:07 +0100 [thread overview]
Message-ID: <YQv3z5Tw4dNPQX+X@redhat.com> (raw)
In-Reply-To: <YQqBTnWKbu+04nM5@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 623 bytes --]
On 04/08/21 13:00 +0100, Jonathan Wakely wrote:
>On 04/08/21 12:56 +0100, Jonathan Wakely wrote:
>>... and container adaptors.
>>
>>This adds the [[nodiscard]] attribute to functions with no side-effects
>>for the sequence containers and their iterators, and the debug versions
>>of those containers, and the container adaptors,
>
>I don't plan to add any more [[nodiscard]] attributes for now, but
>these two commits should demonstrate how to do it for anybody who
>wants to contribute similar patches.
OK, one more change in this vein, adding [[nodiscard]] to <compare>.
Tested powerpc64le-linux, committed to trunk.
[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 10719 bytes --]
commit 8dec72aeb54e98643c0fb3d53768cdb96cf1342a
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Thu Aug 5 14:01:31 2021
libstdc++: Add [[nodiscard]] to <compare>
This adds the [[nodiscard]] attribute to all conversion operators,
comparison operators, call operators and non-member functions in
<compare>. Nothing in this header except constructors has side effects.
Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/ChangeLog:
* libsupc++/compare (partial_ordering, weak_ordering)
(strong_ordering, is_eq, is_neq, is_lt, is_lteq, is_gt, is_gteq)
(compare_three_way, strong_order, weak_order, partial_order)
(compare_strong_order_fallback, compare_weak_order_fallback)
(compare_partial_order_fallback, __detail::__synth3way): Add
nodiscard attribute.
* testsuite/18_support/comparisons/categories/zero_neg.cc: Add
-Wno-unused-result to options.
diff --git a/libstdc++-v3/libsupc++/compare b/libstdc++-v3/libsupc++/compare
index dd0ec5fa36d..faeff641437 100644
--- a/libstdc++-v3/libsupc++/compare
+++ b/libstdc++-v3/libsupc++/compare
@@ -86,49 +86,61 @@ namespace std
static const partial_ordering unordered;
// comparisons
+ [[nodiscard]]
friend constexpr bool
operator==(partial_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v._M_value == 0; }
+ [[nodiscard]]
friend constexpr bool
operator==(partial_ordering, partial_ordering) noexcept = default;
+ [[nodiscard]]
friend constexpr bool
operator< (partial_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v._M_value == -1; }
+ [[nodiscard]]
friend constexpr bool
operator> (partial_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v._M_value == 1; }
+ [[nodiscard]]
friend constexpr bool
operator<=(partial_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v._M_value <= 0; }
+ [[nodiscard]]
friend constexpr bool
operator>=(partial_ordering __v, __cmp_cat::__unspec) noexcept
{ return __cmp_cat::type(__v._M_value & 1) == __v._M_value; }
+ [[nodiscard]]
friend constexpr bool
operator< (__cmp_cat::__unspec, partial_ordering __v) noexcept
{ return __v._M_value == 1; }
+ [[nodiscard]]
friend constexpr bool
operator> (__cmp_cat::__unspec, partial_ordering __v) noexcept
{ return __v._M_value == -1; }
+ [[nodiscard]]
friend constexpr bool
operator<=(__cmp_cat::__unspec, partial_ordering __v) noexcept
{ return __cmp_cat::type(__v._M_value & 1) == __v._M_value; }
+ [[nodiscard]]
friend constexpr bool
operator>=(__cmp_cat::__unspec, partial_ordering __v) noexcept
{ return 0 >= __v._M_value; }
+ [[nodiscard]]
friend constexpr partial_ordering
operator<=>(partial_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v; }
+ [[nodiscard]]
friend constexpr partial_ordering
operator<=>(__cmp_cat::__unspec, partial_ordering __v) noexcept
{
@@ -168,53 +180,66 @@ namespace std
static const weak_ordering equivalent;
static const weak_ordering greater;
+ [[nodiscard]]
constexpr operator partial_ordering() const noexcept
{ return partial_ordering(__cmp_cat::_Ord(_M_value)); }
// comparisons
+ [[nodiscard]]
friend constexpr bool
operator==(weak_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v._M_value == 0; }
+ [[nodiscard]]
friend constexpr bool
operator==(weak_ordering, weak_ordering) noexcept = default;
+ [[nodiscard]]
friend constexpr bool
operator< (weak_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v._M_value < 0; }
+ [[nodiscard]]
friend constexpr bool
operator> (weak_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v._M_value > 0; }
+ [[nodiscard]]
friend constexpr bool
operator<=(weak_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v._M_value <= 0; }
+ [[nodiscard]]
friend constexpr bool
operator>=(weak_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v._M_value >= 0; }
+ [[nodiscard]]
friend constexpr bool
operator< (__cmp_cat::__unspec, weak_ordering __v) noexcept
{ return 0 < __v._M_value; }
+ [[nodiscard]]
friend constexpr bool
operator> (__cmp_cat::__unspec, weak_ordering __v) noexcept
{ return 0 > __v._M_value; }
+ [[nodiscard]]
friend constexpr bool
operator<=(__cmp_cat::__unspec, weak_ordering __v) noexcept
{ return 0 <= __v._M_value; }
+ [[nodiscard]]
friend constexpr bool
operator>=(__cmp_cat::__unspec, weak_ordering __v) noexcept
{ return 0 >= __v._M_value; }
+ [[nodiscard]]
friend constexpr weak_ordering
operator<=>(weak_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v; }
+ [[nodiscard]]
friend constexpr weak_ordering
operator<=>(__cmp_cat::__unspec, weak_ordering __v) noexcept
{ return weak_ordering(__cmp_cat::_Ord(-__v._M_value)); }
@@ -246,56 +271,70 @@ namespace std
static const strong_ordering equivalent;
static const strong_ordering greater;
+ [[nodiscard]]
constexpr operator partial_ordering() const noexcept
{ return partial_ordering(__cmp_cat::_Ord(_M_value)); }
+ [[nodiscard]]
constexpr operator weak_ordering() const noexcept
{ return weak_ordering(__cmp_cat::_Ord(_M_value)); }
// comparisons
+ [[nodiscard]]
friend constexpr bool
operator==(strong_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v._M_value == 0; }
+ [[nodiscard]]
friend constexpr bool
operator==(strong_ordering, strong_ordering) noexcept = default;
+ [[nodiscard]]
friend constexpr bool
operator< (strong_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v._M_value < 0; }
+ [[nodiscard]]
friend constexpr bool
operator> (strong_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v._M_value > 0; }
+ [[nodiscard]]
friend constexpr bool
operator<=(strong_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v._M_value <= 0; }
+ [[nodiscard]]
friend constexpr bool
operator>=(strong_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v._M_value >= 0; }
+ [[nodiscard]]
friend constexpr bool
operator< (__cmp_cat::__unspec, strong_ordering __v) noexcept
{ return 0 < __v._M_value; }
+ [[nodiscard]]
friend constexpr bool
operator> (__cmp_cat::__unspec, strong_ordering __v) noexcept
{ return 0 > __v._M_value; }
+ [[nodiscard]]
friend constexpr bool
operator<=(__cmp_cat::__unspec, strong_ordering __v) noexcept
{ return 0 <= __v._M_value; }
+ [[nodiscard]]
friend constexpr bool
operator>=(__cmp_cat::__unspec, strong_ordering __v) noexcept
{ return 0 >= __v._M_value; }
+ [[nodiscard]]
friend constexpr strong_ordering
operator<=>(strong_ordering __v, __cmp_cat::__unspec) noexcept
{ return __v; }
+ [[nodiscard]]
friend constexpr strong_ordering
operator<=>(__cmp_cat::__unspec, strong_ordering __v) noexcept
{ return strong_ordering(__cmp_cat::_Ord(-__v._M_value)); }
@@ -316,26 +355,32 @@ namespace std
// named comparison functions
+ [[nodiscard]]
constexpr bool
is_eq(partial_ordering __cmp) noexcept
{ return __cmp == 0; }
+ [[nodiscard]]
constexpr bool
is_neq(partial_ordering __cmp) noexcept
{ return __cmp != 0; }
+ [[nodiscard]]
constexpr bool
is_lt (partial_ordering __cmp) noexcept
{ return __cmp < 0; }
+ [[nodiscard]]
constexpr bool
is_lteq(partial_ordering __cmp) noexcept
{ return __cmp <= 0; }
+ [[nodiscard]]
constexpr bool
is_gt (partial_ordering __cmp) noexcept
{ return __cmp > 0; }
+ [[nodiscard]]
constexpr bool
is_gteq(partial_ordering __cmp) noexcept
{ return __cmp >= 0; }
@@ -505,6 +550,7 @@ namespace std
constexpr auto
operator()(_Tp&& __t, _Up&& __u) const
noexcept(noexcept(std::declval<_Tp>() <=> std::declval<_Up>()))
+ [[nodiscard]]
{
if constexpr (__detail::__3way_builtin_ptr_cmp<_Tp, _Up>)
{
@@ -628,6 +674,7 @@ namespace std
constexpr strong_ordering
operator()(_Tp&& __e, _Up&& __f) const
noexcept(_S_noexcept<_Tp, _Up>())
+ [[nodiscard]]
{
/* FIXME:
if constexpr (floating_point<decay_t<_Tp>>)
@@ -675,6 +722,7 @@ namespace std
constexpr weak_ordering
operator()(_Tp&& __e, _Up&& __f) const
noexcept(_S_noexcept<_Tp, _Up>())
+ [[nodiscard]]
{
if constexpr (floating_point<decay_t<_Tp>>)
return __cmp_cust::__fp_weak_ordering(__e, __f);
@@ -720,6 +768,7 @@ namespace std
constexpr partial_ordering
operator()(_Tp&& __e, _Up&& __f) const
noexcept(_S_noexcept<_Tp, _Up>())
+ [[nodiscard]]
{
if constexpr (__adl_partial<_Tp, _Up>)
return partial_ordering(partial_order(static_cast<_Tp&&>(__e),
@@ -761,6 +810,7 @@ namespace std
constexpr strong_ordering
operator()(_Tp&& __e, _Up&& __f) const
noexcept(_S_noexcept<_Tp, _Up>())
+ [[nodiscard]]
{
if constexpr (__strongly_ordered<_Tp, _Up>)
return _Strong_order{}(static_cast<_Tp&&>(__e),
@@ -793,6 +843,7 @@ namespace std
constexpr weak_ordering
operator()(_Tp&& __e, _Up&& __f) const
noexcept(_S_noexcept<_Tp, _Up>())
+ [[nodiscard]]
{
if constexpr (__weakly_ordered<_Tp, _Up>)
return _Weak_order{}(static_cast<_Tp&&>(__e),
@@ -835,6 +886,7 @@ namespace std
constexpr partial_ordering
operator()(_Tp&& __e, _Up&& __f) const
noexcept(_S_noexcept<_Tp, _Up>())
+ [[nodiscard]]
{
if constexpr (__partially_ordered<_Tp, _Up>)
return _Partial_order{}(static_cast<_Tp&&>(__e),
@@ -886,6 +938,7 @@ namespace std
}
template<typename _Tp, typename _Up>
+ [[nodiscard]]
constexpr auto
operator()(const _Tp& __t, const _Up& __u) const
noexcept(_S_noexcept<_Tp, _Up>())
diff --git a/libstdc++-v3/testsuite/18_support/comparisons/categories/zero_neg.cc b/libstdc++-v3/testsuite/18_support/comparisons/categories/zero_neg.cc
index 0ca7de0179c..f912ad399d0 100644
--- a/libstdc++-v3/testsuite/18_support/comparisons/categories/zero_neg.cc
+++ b/libstdc++-v3/testsuite/18_support/comparisons/categories/zero_neg.cc
@@ -15,7 +15,7 @@
// with this library; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
-// { dg-options "-std=gnu++2a" }
+// { dg-options "-std=gnu++2a -Wno-unused-result" }
// { dg-do compile { target c++2a } }
#include <compare>
next prev parent reply other threads:[~2021-08-05 14:38 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-04 11:55 [committed 1/2] libstdc++: Add [[nodiscard]] to iterators and related utilities Jonathan Wakely
2021-08-04 11:56 ` [committed 2/2] libstdc++: Add [[nodiscard]] to sequence containers Jonathan Wakely
2021-08-04 12:00 ` Jonathan Wakely
2021-08-05 12:09 ` Christophe Lyon
2021-08-05 12:13 ` Ville Voutilainen
2021-08-05 14:21 ` Jonathan Wakely
2021-08-05 14:38 ` Jonathan Wakely [this message]
2021-08-05 14:19 ` [committed] libstdc++: Move attributes that follow requires-clauses [PR101782] Jonathan Wakely
2021-08-05 14:27 ` Ville Voutilainen
2021-08-05 14:40 ` Jonathan Wakely
2021-08-05 18:02 ` Jonathan Wakely
2021-08-06 13:29 ` Jonathan Wakely
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=YQv3z5Tw4dNPQX+X@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@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: link
Be 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).