[libcxx-commits] [libcxx] [libc++][chrono] Implement LWG 4274: Allow chrono::hh_mm_ss to be constructed from unsigned durations (PR #209686)

A. Jiang via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 24 04:44:09 PDT 2026


================
@@ -0,0 +1,69 @@
+//===----------------------------------------------------------------------===//
+//
+// 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>
+//
+// template<class Duration>
+// class hh_mm_ss {
+// public:
+//   constexpr explicit hh_mm_ss(Duration d);
+// };
+//
+// LWG4274: The hh_mm_ss constructor supports unsigned durations.
+
+#include <chrono>
+#include <ratio>
+
+// A signed arithmetic-like type used as a custom duration representation.
+struct SignedRep {
+  long long value;
+
+  constexpr explicit SignedRep(long long v = 0) : value(v) {}
+  constexpr operator long long() const { return value; }
+
+  friend constexpr SignedRep operator-(SignedRep v) { return SignedRep{-v.value}; }
+  friend constexpr bool operator<(SignedRep lhs, SignedRep rhs) { return lhs.value < rhs.value; }
+};
+
+int main(int, char**) {
+  {
+    // Tests construction from a duration with an unsigned representation.
+    using Duration = std::chrono::duration<unsigned, std::milli>;
+
+    // 1 hour + 1 minute + 1 second + 1 millisecond
+    constexpr Duration d{3'661'001};
+    constexpr std::chrono::hh_mm_ss<Duration> hms{d};
+
+    static_assert(!hms.is_negative());
+    static_assert(hms.hours() == std::chrono::hours{1});
+    static_assert(hms.minutes() == std::chrono::minutes{1});
+    static_assert(hms.seconds() == std::chrono::seconds{1});
+    static_assert(hms.subseconds() == std::chrono::milliseconds{1});
+    static_assert(hms.to_duration() == std::chrono::milliseconds{3'661'001});
----------------
frederick-vs-ja wrote:

Previously we didn't concentratedly test the constructor. Would you mind to follow the existing style which tests these member functions in different test files?

Note that using such a duration is already requiring that the constructor call to be well-formed.

https://github.com/llvm/llvm-project/pull/209686


More information about the libcxx-commits mailing list