[libcxx-commits] [PATCH] D118902: [libc++] Fix chrono::duration constructor constraint

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Feb 3 15:07:36 PST 2022


This revision was not accepted when it landed; it landed in state "Needs Review".
This revision was automatically updated to reflect the committed changes.
Closed by commit rGeaadc451566f: [libc++] Fix chrono::duration constructor constraint (authored by tiagoma, committed by ldionne).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D118902/new/

https://reviews.llvm.org/D118902

Files:
  libcxx/include/__chrono/duration.h
  libcxx/test/std/utilities/time/time.duration/time.duration.cons/rep.pass.cpp


Index: libcxx/test/std/utilities/time/time.duration/time.duration.cons/rep.pass.cpp
===================================================================
--- libcxx/test/std/utilities/time/time.duration/time.duration.cons/rep.pass.cpp
+++ libcxx/test/std/utilities/time/time.duration/time.duration.cons/rep.pass.cpp
@@ -20,24 +20,45 @@
 #include "test_macros.h"
 #include "../../rep.h"
 
+#if TEST_STD_VER >= 11
+struct NotValueConvertible {
+    operator int() const&& = delete;
+    constexpr operator int() const& { return 1; }
+};
+#endif
+
 template <class D, class R>
-void
-test(R r)
-{
+TEST_CONSTEXPR_CXX14 void check(R r) {
     D d(r);
     assert(d.count() == r);
+}
+
+TEST_CONSTEXPR_CXX14 bool test() {
+    check<std::chrono::duration<int> >(5);
+    check<std::chrono::duration<int, std::ratio<3, 2> > >(5);
+    check<std::chrono::duration<Rep, std::ratio<3, 2> > >(Rep(3));
+    check<std::chrono::duration<double, std::ratio<2, 3> > >(5.5);
+
+    // test for [time.duration.cons]/1
 #if TEST_STD_VER >= 11
-    constexpr D d2(R(2));
-    static_assert(d2.count() == 2, "");
+    check<std::chrono::duration<int> >(NotValueConvertible());
 #endif
+
+    return true;
 }
 
-int main(int, char**)
-{
-    test<std::chrono::duration<int> >(5);
-    test<std::chrono::duration<int, std::ratio<3, 2> > >(5);
-    test<std::chrono::duration<Rep, std::ratio<3, 2> > >(Rep(3));
-    test<std::chrono::duration<double, std::ratio<2, 3> > >(5.5);
+int main(int, char**) {
+    test();
+#if TEST_STD_VER > 11
+    static_assert(test(), "");
+#endif
 
-  return 0;
+    // Basic test for constexpr-friendliness in C++11
+#if TEST_STD_VER >= 11
+    {
+        constexpr std::chrono::duration<int> d(5);
+        static_assert(d.count() == 5, "");
+    }
+#endif
+    return 0;
 }
Index: libcxx/include/__chrono/duration.h
===================================================================
--- libcxx/include/__chrono/duration.h
+++ libcxx/include/__chrono/duration.h
@@ -251,7 +251,7 @@
         explicit duration(const _Rep2& __r,
             typename enable_if
             <
-               is_convertible<_Rep2, rep>::value &&
+               is_convertible<const _Rep2&, rep>::value &&
                (treat_as_floating_point<rep>::value ||
                !treat_as_floating_point<_Rep2>::value)
             >::type* = nullptr)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118902.405809.patch
Type: text/x-patch
Size: 2343 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220203/2304f724/attachment.bin>


More information about the libcxx-commits mailing list