[gcc r12-2768] libstdc++: Add [[nodiscard]] to <compare>

Jonathan Wakely redi@gcc.gnu.org
Thu Aug 5 14:17:40 GMT 2021


https://gcc.gnu.org/g:8dec72aeb54e98643c0fb3d53768cdb96cf1342a

commit r12-2768-g8dec72aeb54e98643c0fb3d53768cdb96cf1342a
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Aug 5 14:01:31 2021 +0100

    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:
---
 libstdc++-v3/libsupc++/compare                     | 53 ++++++++++++++++++++++
 .../18_support/comparisons/categories/zero_neg.cc  |  2 +-
 2 files changed, 54 insertions(+), 1 deletion(-)

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>


More information about the Libstdc++-cvs mailing list