From a17e97e6778b2cd4114052faf6ee25db330ef405 Mon Sep 17 00:00:00 2001 From: maflcko <6399679+maflcko@users.noreply.github.com> Date: Wed, 11 Jun 2025 08:43:23 +0200 Subject: [PATCH] [libc++] Add missing C++20 [time.point.arithmetic] (#143165) This was part of https://wg21.link/p0355r7, but apparently never implemented. --------- Co-authored-by: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Co-authored-by: Hristo Hristov --- libcxx/include/__chrono/time_point.h | 13 +++++++ libcxx/include/chrono | 5 +++ .../time.point.arithmetic/op_++.pass.cpp | 35 +++++++++++++++++++ .../time.point.arithmetic/op_++int.pass.cpp | 35 +++++++++++++++++++ .../time.point.arithmetic/op_--.pass.cpp | 35 +++++++++++++++++++ .../time.point.arithmetic/op_--int.pass.cpp | 35 +++++++++++++++++++ 6 files changed, 158 insertions(+) create mode 100644 libcxx/test/std/time/time.point/time.point.arithmetic/op_++.pass.cpp create mode 100644 libcxx/test/std/time/time.point/time.point.arithmetic/op_++int.pass.cpp create mode 100644 libcxx/test/std/time/time.point/time.point.arithmetic/op_--.pass.cpp create mode 100644 libcxx/test/std/time/time.point/time.point.arithmetic/op_--int.pass.cpp diff --git a/libcxx/include/__chrono/time_point.h b/libcxx/include/__chrono/time_point.h index 6b866b882f89..fc4408d23dbf 100644 --- a/libcxx/include/__chrono/time_point.h +++ b/libcxx/include/__chrono/time_point.h @@ -58,6 +58,19 @@ public: // arithmetic +#if _LIBCPP_STD_VER >= 20 + _LIBCPP_HIDE_FROM_ABI constexpr time_point& operator++() { + ++__d_; + return *this; + } + _LIBCPP_HIDE_FROM_ABI constexpr time_point operator++(int) { return time_point{__d_++}; } + _LIBCPP_HIDE_FROM_ABI constexpr time_point& operator--() { + --__d_; + return *this; + } + _LIBCPP_HIDE_FROM_ABI constexpr time_point operator--(int) { return time_point{__d_--}; } +#endif // _LIBCPP_STD_VER >= 20 + _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 time_point& operator+=(const duration& __d) { __d_ += __d; return *this; diff --git a/libcxx/include/chrono b/libcxx/include/chrono index cd9b98872083..82e99a31bcc9 100644 --- a/libcxx/include/chrono +++ b/libcxx/include/chrono @@ -132,6 +132,11 @@ public: // arithmetic + constexpr time_point& operator++(); // C++20 + constexpr time_point operator++(int); // C++20 + constexpr time_point& operator--(); // C++20 + constexpr time_point operator--(int); // C++20 + time_point& operator+=(const duration& d); // constexpr in C++17 time_point& operator-=(const duration& d); // constexpr in C++17 diff --git a/libcxx/test/std/time/time.point/time.point.arithmetic/op_++.pass.cpp b/libcxx/test/std/time/time.point/time.point.arithmetic/op_++.pass.cpp new file mode 100644 index 000000000000..e035d7ef4fa0 --- /dev/null +++ b/libcxx/test/std/time/time.point/time.point.arithmetic/op_++.pass.cpp @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// REQUIRES: std-at-least-c++20 + +// + +// time_point + +// constexpr time_point& operator++(); + +#include +#include + +#include "test_macros.h" + +constexpr bool test() { + using Clock = std::chrono::system_clock; + using Duration = std::chrono::milliseconds; + std::chrono::time_point t{Duration{5}}; + std::chrono::time_point& tref{++t}; + assert(&tref == &t); + assert(tref.time_since_epoch() == Duration{6}); + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + return 0; +} diff --git a/libcxx/test/std/time/time.point/time.point.arithmetic/op_++int.pass.cpp b/libcxx/test/std/time/time.point/time.point.arithmetic/op_++int.pass.cpp new file mode 100644 index 000000000000..5304d37d5c36 --- /dev/null +++ b/libcxx/test/std/time/time.point/time.point.arithmetic/op_++int.pass.cpp @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// REQUIRES: std-at-least-c++20 + +// + +// time_point + +// constexpr time_point operator++(int); + +#include +#include + +#include "test_macros.h" + +constexpr bool test() { + using Clock = std::chrono::system_clock; + using Duration = std::chrono::milliseconds; + std::chrono::time_point t1{Duration{3}}; + std::chrono::time_point t2{t1++}; + assert(t1.time_since_epoch() == Duration{4}); + assert(t2.time_since_epoch() == Duration{3}); + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + return 0; +} diff --git a/libcxx/test/std/time/time.point/time.point.arithmetic/op_--.pass.cpp b/libcxx/test/std/time/time.point/time.point.arithmetic/op_--.pass.cpp new file mode 100644 index 000000000000..915156fcc6b8 --- /dev/null +++ b/libcxx/test/std/time/time.point/time.point.arithmetic/op_--.pass.cpp @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// REQUIRES: std-at-least-c++20 + +// + +// time_point + +// constexpr time_point& operator--(); + +#include +#include + +#include "test_macros.h" + +constexpr bool test() { + using Clock = std::chrono::system_clock; + using Duration = std::chrono::milliseconds; + std::chrono::time_point t{Duration{5}}; + std::chrono::time_point& tref{--t}; + assert(&tref == &t); + assert(tref.time_since_epoch() == Duration{4}); + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + return 0; +} diff --git a/libcxx/test/std/time/time.point/time.point.arithmetic/op_--int.pass.cpp b/libcxx/test/std/time/time.point/time.point.arithmetic/op_--int.pass.cpp new file mode 100644 index 000000000000..cc5f462106bb --- /dev/null +++ b/libcxx/test/std/time/time.point/time.point.arithmetic/op_--int.pass.cpp @@ -0,0 +1,35 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// REQUIRES: std-at-least-c++20 + +// + +// time_point + +// constexpr time_point operator--(int); + +#include +#include + +#include "test_macros.h" + +constexpr bool test() { + using Clock = std::chrono::system_clock; + using Duration = std::chrono::milliseconds; + std::chrono::time_point t1{Duration{3}}; + std::chrono::time_point t2{t1--}; + assert(t1.time_since_epoch() == Duration{2}); + assert(t2.time_since_epoch() == Duration{3}); + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + return 0; +}