[libcxx-commits] [libcxx] fd36a3d - [libc++][chrono] Adds operator<=> for day.
Mark de Wever via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jul 13 08:20:37 PDT 2022
Author: Mark de Wever
Date: 2022-07-13T17:20:31+02:00
New Revision: fd36a3d48d0554930db0381286f4cad3e9c7c1ea
URL: https://github.com/llvm/llvm-project/commit/fd36a3d48d0554930db0381286f4cad3e9c7c1ea
DIFF: https://github.com/llvm/llvm-project/commit/fd36a3d48d0554930db0381286f4cad3e9c7c1ea.diff
LOG: [libc++][chrono] Adds operator<=> for day.
Since the calendar classes were introduced in C++20 there's no need to
keep the old comparison operators.
This commit does the day calender class, the other calendar classes will
be in a followup commit.
Implements parts of:
- P1614R2 The mothership has landed
Reviewed By: #libc, ldionne
Differential Revision: https://reviews.llvm.org/D128603
Added:
Modified:
libcxx/include/__chrono/day.h
libcxx/include/chrono
libcxx/test/std/time/time.cal/time.cal.day/time.cal.day.nonmembers/comparisons.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/include/__chrono/day.h b/libcxx/include/__chrono/day.h
index 7e425558e359f..d9fa4ffbc45e9 100644
--- a/libcxx/include/__chrono/day.h
+++ b/libcxx/include/__chrono/day.h
@@ -12,6 +12,7 @@
#include <__chrono/duration.h>
#include <__config>
+#include <compare>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
@@ -45,25 +46,9 @@ _LIBCPP_HIDE_FROM_ABI inline constexpr
bool operator==(const day& __lhs, const day& __rhs) noexcept
{ return static_cast<unsigned>(__lhs) == static_cast<unsigned>(__rhs); }
-_LIBCPP_HIDE_FROM_ABI inline constexpr
-bool operator!=(const day& __lhs, const day& __rhs) noexcept
-{ return !(__lhs == __rhs); }
-
-_LIBCPP_HIDE_FROM_ABI inline constexpr
-bool operator< (const day& __lhs, const day& __rhs) noexcept
-{ return static_cast<unsigned>(__lhs) < static_cast<unsigned>(__rhs); }
-
-_LIBCPP_HIDE_FROM_ABI inline constexpr
-bool operator> (const day& __lhs, const day& __rhs) noexcept
-{ return __rhs < __lhs; }
-
-_LIBCPP_HIDE_FROM_ABI inline constexpr
-bool operator<=(const day& __lhs, const day& __rhs) noexcept
-{ return !(__rhs < __lhs);}
-
-_LIBCPP_HIDE_FROM_ABI inline constexpr
-bool operator>=(const day& __lhs, const day& __rhs) noexcept
-{ return !(__lhs < __rhs); }
+_LIBCPP_HIDE_FROM_ABI constexpr strong_ordering operator<=>(const day& __lhs, const day& __rhs) noexcept {
+ return static_cast<unsigned>(__lhs) <=> static_cast<unsigned>(__rhs);
+}
_LIBCPP_HIDE_FROM_ABI inline constexpr
day operator+ (const day& __lhs, const days& __rhs) noexcept
diff --git a/libcxx/include/chrono b/libcxx/include/chrono
index 9185d74c09c41..2af5fbcc51651 100644
--- a/libcxx/include/chrono
+++ b/libcxx/include/chrono
@@ -13,6 +13,8 @@
/*
chrono synopsis
+#include <compare> // C++20
+
namespace std
{
namespace chrono
@@ -325,11 +327,7 @@ struct last_spec;
class day;
constexpr bool operator==(const day& x, const day& y) noexcept;
-constexpr bool operator!=(const day& x, const day& y) noexcept;
-constexpr bool operator< (const day& x, const day& y) noexcept;
-constexpr bool operator> (const day& x, const day& y) noexcept;
-constexpr bool operator<=(const day& x, const day& y) noexcept;
-constexpr bool operator>=(const day& x, const day& y) noexcept;
+constexpr strong_ordering operator<=>(const day& x, const day& y) noexcept;
constexpr day operator+(const day& x, const days& y) noexcept;
constexpr day operator+(const days& x, const day& y) noexcept;
constexpr day operator-(const day& x, const days& y) noexcept;
@@ -715,9 +713,11 @@ constexpr chrono::year operator ""y(unsigned lo
#include <__chrono/year_month_day.h>
#include <__chrono/year_month_weekday.h>
#include <__config>
-#include <compare>
#include <version>
+// standard-mandated includes
+#include <compare>
+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
diff --git a/libcxx/test/std/time/time.cal/time.cal.day/time.cal.day.nonmembers/comparisons.pass.cpp b/libcxx/test/std/time/time.cal/time.cal.day/time.cal.day.nonmembers/comparisons.pass.cpp
index 1b5b712095ce7..959565a0a55a6 100644
--- a/libcxx/test/std/time/time.cal/time.cal.day/time.cal.day.nonmembers/comparisons.pass.cpp
+++ b/libcxx/test/std/time/time.cal/time.cal.day/time.cal.day.nonmembers/comparisons.pass.cpp
@@ -12,9 +12,8 @@
// constexpr bool operator==(const day& x, const day& y) noexcept;
// Returns: unsigned{x} == unsigned{y}.
-// constexpr bool operator<(const day& x, const day& y) noexcept;
-// Returns: unsigned{x} < unsigned{y}.
-
+// constexpr strong_ordering operator<=>(const day& x, const day& y) noexcept;
+// Returns: unsigned{x} <=> unsigned{y}.
#include <chrono>
#include <type_traits>
@@ -23,23 +22,30 @@
#include "test_macros.h"
#include "test_comparisons.h"
-int main(int, char**)
-{
- using day = std::chrono::day;
+constexpr bool test() {
+ using day = std::chrono::day;
+
+ // Validate invalid values. The range [0, 255] is guaranteed to be allowed.
+ assert(testOrderValues<day>(0U, 0U));
+ assert(testOrderValues<day>(0U, 1U));
+ assert(testOrderValues<day>(254U, 255U));
+ assert(testOrderValues<day>(255U, 255U));
- AssertComparisonsAreNoexcept<day>();
- AssertComparisonsReturnBool<day>();
+ // Validate some valid values.
+ for (unsigned i = 1; i < 10; ++i)
+ for (unsigned j = 1; j < 10; ++j)
+ assert(testOrderValues<day>(i, j));
- static_assert(testComparisonsValues<day>(0U, 0U), "");
- static_assert(testComparisonsValues<day>(0U, 1U), "");
+ return true;
+}
- // Some 'ok' values as well
- static_assert(testComparisonsValues<day>( 5U, 5U), "");
- static_assert(testComparisonsValues<day>( 5U, 10U), "");
+int main(int, char**) {
+ using day = std::chrono::day;
+ AssertOrderAreNoexcept<day>();
+ AssertOrderReturn<std::strong_ordering, day>();
- for (unsigned i = 1; i < 10; ++i)
- for (unsigned j = 1; j < 10; ++j)
- assert(testComparisonsValues<day>(i, j));
+ test();
+ static_assert(test());
return 0;
}
More information about the libcxx-commits
mailing list