[libc++][spaceship] Implement operator<=> for duration

Implements parts of [[ https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1614r2.html | P1614R2 ]]
Implemented `operator<=>` for `std::chrono::duration`

Reviewed By: #libc, Mordante

Differential Revision: https://reviews.llvm.org/D145881
This commit is contained in:
Hristo Hristov 2023-03-18 12:54:17 +02:00
parent 32baf5c1c2
commit 83542e4764
4 changed files with 103 additions and 2 deletions

View File

@ -52,7 +52,7 @@ Section,Description,Dependencies,Assignee,Complete
| `[range.iota.iterator] <https://wg21.link/range.iota.iterator>`_,| `ranges::iota_view::iterator <https://reviews.llvm.org/D110774>`_,[concepts.cmp],Arthur O'Dwyer,|Complete|
| `[range.transform.iterator] <https://wg21.link/range.transform.iterator>`_,| `ranges::transform_view::iterator <https://reviews.llvm.org/D110774>`_,[concepts.cmp],Arthur O'Dwyer,|Complete|
| `[range.elements.iterator] <https://wg21.link/range.elements.iterator>`_,| ranges::elements_view::iterator,[concepts.cmp],Hui Xie,|Complete|
| `[time.duration.comparisons] <https://wg21.link/time.duration.comparisons>`_, "chrono::duration", None, Mark de Wever, |Not Started|
| `[time.duration.comparisons] <https://wg21.link/time.duration.comparisons>`_, `chrono::duration <https://reviews.llvm.org/D145881>`_, None, Hristo Hristov, |Complete|
| `[time.point.comparisons] <https://wg21.link/time.point.comparisons>`_, "chrono::time_point", None, Mark de Wever, |Not Started|
"| `[time.cal.day.nonmembers] <https://wg21.link/time.cal.day.nonmembers>`_
| `[time.cal.month.nonmembers] <https://wg21.link/time.cal.month.nonmembers>`_

1 Section Description Dependencies Assignee Complete
52
53
54
55
56
57
58

View File

@ -10,6 +10,8 @@
#ifndef _LIBCPP___CHRONO_DURATION_H
#define _LIBCPP___CHRONO_DURATION_H
#include <__compare/ordering.h>
#include <__compare/three_way_comparable.h>
#include <__config>
#include <__type_traits/common_type.h>
#include <__type_traits/enable_if.h>
@ -343,6 +345,8 @@ operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period
return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
}
#if _LIBCPP_STD_VER <= 17
// Duration !=
template <class _Rep1, class _Period1, class _Rep2, class _Period2>
@ -354,6 +358,8 @@ operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period
return !(__lhs == __rhs);
}
#endif // _LIBCPP_STD_VER <= 17
// Duration <
template <class _LhsDuration, class _RhsDuration>
@ -417,6 +423,20 @@ operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period
return !(__lhs < __rhs);
}
#if _LIBCPP_STD_VER >= 20
template<class _Rep1, class _Period1, class _Rep2, class _Period2>
requires three_way_comparable<common_type_t<_Rep1, _Rep2>>
_LIBCPP_HIDE_FROM_ABI
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();
}
#endif // _LIBCPP_STD_VER >= 20
// Duration +
template <class _Rep1, class _Period1, class _Rep2, class _Period2>

View File

@ -182,7 +182,7 @@ template <class Rep1, class Period1, class Rep2, class Period2>
bool operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr
bool operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
bool operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); // removed in C++20
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr
bool operator< (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
@ -195,6 +195,10 @@ template <class Rep1, class Period1, class Rep2, class Period2>
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr
bool operator>=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
template<class Rep1, class Period1, class Rep2, class Period2>
requires three_way_comparable<typename CT::rep>
constexpr auto operator<=>(const duration<Rep1, Period1>& lhs,
const duration<Rep2, Period2>& rhs); // since C++20
// duration_cast
template <class ToDuration, class Rep, class Period>

View File

@ -0,0 +1,77 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// <chrono>
// duration
// template<class Rep1, class Period1, class Rep2, class Period2>
// requires ThreeWayComparable<typename CT::rep>
// constexpr auto operator<=>(const duration<Rep1, Period1>& lhs,
// const duration<Rep2, Period2>& rhs);
#include <cassert>
#include <chrono>
#include <ratio>
#include "test_comparisons.h"
constexpr bool test() {
{
std::chrono::seconds s1(3);
std::chrono::seconds s2(3);
assert((s1 <=> s2) == std::strong_ordering::equal);
assert(testOrder(s1, s2, std::strong_ordering::equal));
}
{
std::chrono::seconds s1(3);
std::chrono::seconds s2(4);
assert((s1 <=> s2) == std::strong_ordering::less);
assert(testOrder(s1, s2, std::strong_ordering::less));
}
{
std::chrono::milliseconds s1(3);
std::chrono::microseconds s2(3000);
assert((s1 <=> s2) == std::strong_ordering::equal);
assert(testOrder(s1, s2, std::strong_ordering::equal));
}
{
std::chrono::milliseconds s1(3);
std::chrono::microseconds s2(4000);
assert((s1 <=> s2) == std::strong_ordering::less);
assert(testOrder(s1, s2, std::strong_ordering::less));
}
{
std::chrono::duration<int, std::ratio<2, 3>> s1(9);
std::chrono::duration<int, std::ratio<3, 5>> s2(10);
assert((s1 <=> s2) == std::strong_ordering::equal);
assert(testOrder(s1, s2, std::strong_ordering::equal));
}
{
std::chrono::duration<int, std::ratio<2, 3>> s1(10);
std::chrono::duration<int, std::ratio<3, 5>> s2(9);
assert((s1 <=> s2) == std::strong_ordering::greater);
assert(testOrder(s1, s2, std::strong_ordering::greater));
}
{
std::chrono::duration<int, std::ratio<2, 3>> s1(9);
std::chrono::duration<double, std::ratio<3, 5>> s2(10.1);
assert((s1 <=> s2) == std::strong_ordering::less);
assert(testOrder(s1, s2, std::strong_ordering::less));
}
return true;
}
int main(int, char**) {
assert(test());
static_assert(test());
return 0;
}