Skip to content

Commit

Permalink
C++20 compatibility for time.h.
Browse files Browse the repository at this point in the history
Fixes two issues:
* TimeBase/TimeTicks were not trivially copyable, and thus could not be
  used in atomics.
* Ambiguous comparison operators due to inexact matches (base vs.
  derived classes).

Bug: 1284275
Change-Id: I37294ce1e2a4e9069f6f1b1d32b9ad1f7ebdf7db
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3617151
Commit-Queue: Gabriel Charette <gab@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Reviewed-by: Gabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1001038}
  • Loading branch information
pkasting authored and Chromium LUCI CQ committed May 9, 2022
1 parent 9bbe6cc commit 3a00e67
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions base/time/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,8 @@ class TimeBase {
// the other subclasses can vary each time the application is restarted.
constexpr TimeDelta since_origin() const;

constexpr TimeClass& operator=(TimeClass other) {
us_ = other.us_;
return *(static_cast<TimeClass*>(this));
}

// Compute the difference between two times.
constexpr TimeDelta operator-(TimeClass other) const;
constexpr TimeDelta operator-(const TimeBase<TimeClass>& other) const;

// Return a new time modified by some delta.
constexpr TimeClass operator+(TimeDelta delta) const;
Expand All @@ -463,12 +458,24 @@ class TimeBase {
}

// Comparison operators
constexpr bool operator==(TimeClass other) const { return us_ == other.us_; }
constexpr bool operator!=(TimeClass other) const { return us_ != other.us_; }
constexpr bool operator<(TimeClass other) const { return us_ < other.us_; }
constexpr bool operator<=(TimeClass other) const { return us_ <= other.us_; }
constexpr bool operator>(TimeClass other) const { return us_ > other.us_; }
constexpr bool operator>=(TimeClass other) const { return us_ >= other.us_; }
constexpr bool operator==(const TimeBase<TimeClass>& other) const {
return us_ == other.us_;
}
constexpr bool operator!=(const TimeBase<TimeClass>& other) const {
return us_ != other.us_;
}
constexpr bool operator<(const TimeBase<TimeClass>& other) const {
return us_ < other.us_;
}
constexpr bool operator<=(const TimeBase<TimeClass>& other) const {
return us_ <= other.us_;
}
constexpr bool operator>(const TimeBase<TimeClass>& other) const {
return us_ > other.us_;
}
constexpr bool operator>=(const TimeBase<TimeClass>& other) const {
return us_ >= other.us_;
}

protected:
constexpr explicit TimeBase(int64_t us) : us_(us) {}
Expand Down Expand Up @@ -947,7 +954,8 @@ constexpr TimeDelta TimeBase<TimeClass>::since_origin() const {
}

template <class TimeClass>
constexpr TimeDelta TimeBase<TimeClass>::operator-(TimeClass other) const {
constexpr TimeDelta TimeBase<TimeClass>::operator-(
const TimeBase<TimeClass>& other) const {
return Microseconds(us_ - other.us_);
}

Expand Down

0 comments on commit 3a00e67

Please sign in to comment.