libstdc++: Add comparison operators to <chrono> types
authorJonathan Wakely <jwakely@redhat.com>
Fri, 17 Apr 2020 23:47:45 +0000 (00:47 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 17 Apr 2020 23:47:45 +0000 (00:47 +0100)
Some more C++20 changes from P1614R2, "The Mothership has Landed".

* include/std/chrono (duration, time_point): Define operator<=> and
remove redundant operator!= for C++20.
* testsuite/20_util/duration/comparison_operators/three_way.cc: New
test.
* testsuite/20_util/time_point/comparison_operators/three_way.cc: New
test.

libstdc++-v3/ChangeLog
libstdc++-v3/include/std/chrono
libstdc++-v3/testsuite/20_util/duration/comparison_operators/three_way.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/time_point/comparison_operators/three_way.cc [new file with mode: 0644]

index 64c862c1d03a55319808568aa5a1079b01623938..40345cc7044f10c0082d992df6cd34d7ad528cf0 100644 (file)
@@ -1,5 +1,12 @@
 2020-04-18  Jonathan Wakely  <jwakely@redhat.com>
 
+       * include/std/chrono (duration, time_point): Define operator<=> and
+       remove redundant operator!= for C++20.
+       * testsuite/20_util/duration/comparison_operators/three_way.cc: New
+       test.
+       * testsuite/20_util/time_point/comparison_operators/three_way.cc: New
+       test.
+
        * testsuite/util/native_type/native_priority_queue.hpp: Use
        allocator_traits to rebind allocator.
 
index 514926c5c05b227e6509e63a63902d8704dae420..6d78f32ac783005725e466ec035a58d9271a55f3 100644 (file)
@@ -43,6 +43,7 @@
 #include <bits/parse_numbers.h> // for literals support.
 #if __cplusplus > 201703L
 # include <concepts>
+# include <compare>
 #endif
 
 namespace std _GLIBCXX_VISIBILITY(default)
@@ -668,12 +669,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        return __ct(__lhs).count() < __ct(__rhs).count();
       }
 
+#if __cpp_lib_three_way_comparison
+    template<typename _Rep1, typename _Period1,
+            typename _Rep2, typename _Period2>
+      requires three_way_comparable<common_type_t<_Rep1, _Rep2>>
+      constexpr auto
+      operator<=>(const duration<_Rep1, _Period1>& __lhs,
+                 const duration<_Rep2, _Period2>& __rhs)
+      {
+       using __ct = common_type_t<duration<_Rep1, _Period1>,
+                                  duration<_Rep2, _Period2>>;
+       return __ct(__lhs).count() <=> __ct(__rhs).count();
+      }
+#else
     template<typename _Rep1, typename _Period1,
             typename _Rep2, typename _Period2>
       constexpr bool
       operator!=(const duration<_Rep1, _Period1>& __lhs,
                 const duration<_Rep2, _Period2>& __rhs)
       { return !(__lhs == __rhs); }
+#endif
 
     template<typename _Rep1, typename _Period1,
             typename _Rep2, typename _Period2>
@@ -903,11 +918,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
                 const time_point<_Clock, _Dur2>& __rhs)
       { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
 
+#if __cpp_lib_three_way_comparison
+    template<typename _Clock, typename _Dur1,
+            three_way_comparable_with<_Dur1> _Dur2>
+      constexpr auto
+      operator<=>(const time_point<_Clock, _Dur1>& __lhs,
+                 const time_point<_Clock, _Dur2>& __rhs)
+      { return __lhs.time_since_epoch() <=> __rhs.time_since_epoch(); }
+#else
     template<typename _Clock, typename _Dur1, typename _Dur2>
       constexpr bool
       operator!=(const time_point<_Clock, _Dur1>& __lhs,
                 const time_point<_Clock, _Dur2>& __rhs)
       { return !(__lhs == __rhs); }
+#endif
 
     template<typename _Clock, typename _Dur1, typename _Dur2>
       constexpr bool
diff --git a/libstdc++-v3/testsuite/20_util/duration/comparison_operators/three_way.cc b/libstdc++-v3/testsuite/20_util/duration/comparison_operators/three_way.cc
new file mode 100644 (file)
index 0000000..12c20f8
--- /dev/null
@@ -0,0 +1,62 @@
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// 20.8.3 Class template duration [time.duration]
+
+#include <chrono>
+#include <testsuite_hooks.h>
+
+// C++20 27.5.6 Comparisons [time.duration.comparisons]
+
+void
+test01()
+{
+  using namespace std::chrono;
+
+  duration<int> d0(12);
+  duration<int> d1(3);
+  duration<long> d2(3);
+
+  VERIFY(d1 < d0);
+  VERIFY(d0 > d1);
+  VERIFY( std::is_lt(d1 <=> d0) );
+  VERIFY( std::is_gt(d0 <=> d1) );
+
+  VERIFY(d0 != d1);
+  VERIFY(d1 == d2);
+  VERIFY( std::is_neq(d0 <=> d1) );
+  VERIFY( std::is_eq(d1 <=> d2) );
+
+  VERIFY(d1 <= d2);
+  VERIFY(d1 >= d2);
+  VERIFY( std::is_lteq(d1 <=> d2) );
+  VERIFY( std::is_gteq(d1 <=> d2) );
+
+  VERIFY(d1 <= d0);
+  VERIFY(d0 >= d1);
+  VERIFY( std::is_lteq(d1 <=> d0) );
+  VERIFY( std::is_gteq(d0 <=> d1) );
+}
+
+int
+main()
+{
+  test01();
+}
diff --git a/libstdc++-v3/testsuite/20_util/time_point/comparison_operators/three_way.cc b/libstdc++-v3/testsuite/20_util/time_point/comparison_operators/three_way.cc
new file mode 100644 (file)
index 0000000..b753bcf
--- /dev/null
@@ -0,0 +1,41 @@
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+// Copyright (C) 2020 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+#include <chrono>
+#include <testsuite_hooks.h>
+
+// C++20 27.6.6 Comparisons [time.point.comparisons]
+
+void
+test01()
+{
+  using namespace std::chrono;
+
+  auto ns = system_clock::now();
+  auto s = time_point_cast<seconds>(ns + seconds(2));
+
+  VERIFY( s != ns );
+  VERIFY( std::is_lt(ns <=> s) );
+}
+
+int main()
+{
+  test01();
+}