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.
This commit is contained in:
parent
03d47da7e1
commit
8dec72aeb5
2 changed files with 54 additions and 1 deletions
|
@ -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>())
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Add table
Reference in a new issue