Skip to content

[libc++] Add missing C++20 [time.point.arithmetic] #143165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions libcxx/include/__chrono/time_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ class time_point {

// 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;
Expand Down
5 changes: 5 additions & 0 deletions libcxx/include/chrono
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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

// <chrono>

// time_point

// constexpr time_point& operator++();

#include <cassert>
#include <chrono>

#include "test_macros.h"

constexpr bool test() {
using Clock = std::chrono::system_clock;
using Duration = std::chrono::milliseconds;
std::chrono::time_point<Clock, Duration> t{Duration{5}};
std::chrono::time_point<Clock, Duration>& tref{++t};
assert(&tref == &t);
assert(tref.time_since_epoch() == Duration{6});
return true;
}

int main(int, char**) {
test();
static_assert(test());
return 0;
}
Original file line number Diff line number Diff line change
@@ -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

// <chrono>

// time_point

// constexpr time_point operator++(int);

#include <cassert>
#include <chrono>

#include "test_macros.h"

constexpr bool test() {
using Clock = std::chrono::system_clock;
using Duration = std::chrono::milliseconds;
std::chrono::time_point<Clock, Duration> t1{Duration{3}};
std::chrono::time_point<Clock, Duration> 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;
}
Original file line number Diff line number Diff line change
@@ -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

// <chrono>

// time_point

// constexpr time_point& operator--();

#include <cassert>
#include <chrono>

#include "test_macros.h"

constexpr bool test() {
using Clock = std::chrono::system_clock;
using Duration = std::chrono::milliseconds;
std::chrono::time_point<Clock, Duration> t{Duration{5}};
std::chrono::time_point<Clock, Duration>& tref{--t};
assert(&tref == &t);
assert(tref.time_since_epoch() == Duration{4});
return true;
}

int main(int, char**) {
test();
static_assert(test());
return 0;
}
Original file line number Diff line number Diff line change
@@ -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

// <chrono>

// time_point

// constexpr time_point operator--(int);

#include <cassert>
#include <chrono>

#include "test_macros.h"

constexpr bool test() {
using Clock = std::chrono::system_clock;
using Duration = std::chrono::milliseconds;
std::chrono::time_point<Clock, Duration> t1{Duration{3}};
std::chrono::time_point<Clock, Duration> 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;
}
Loading