]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR libstdc++/81468 constrain std::chrono::time_point constructor
authorJonathan Wakely <jwakely@redhat.com>
Wed, 13 Sep 2017 14:07:44 +0000 (15:07 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Wed, 13 Sep 2017 14:07:44 +0000 (15:07 +0100)
PR libstdc++/81468
* include/std/chrono (__enable_if_is_duration)
(__disable_if_is_duration): New alias templates to simplify SFINAE.
(duration_cast, floor, ceil): Use __enable_if_is_duration.
(duration::__is_float, duration::__is_harmonic): New alias templates
to simplify SFINAE.
(duration::duration(const _Rep2&)): Use _Require, __is_float and
__is_harmonic.
(duration::duration(const duration<_Rep2, _Period2>&)): Likewise.
(__common_rep_type): Remove, replace with ...
(__common_rep_t): New alias template.
(operator*, operator/, operator%): Use __common_rep_t and
__disable_if_is_duration.
(time_point::time_point(const time_point<clock, _Dur2>&)): Add missing
constraint from LWG DR 1177.
* testsuite/20_util/duration/cons/dr1177.cc: New.
* testsuite/20_util/duration/literals/range.cc: Update dg-error line.
* testsuite/20_util/duration/requirements/typedefs_neg1.cc: Likewise.
* testsuite/20_util/duration/requirements/typedefs_neg2.cc: Likewise.
* testsuite/20_util/duration/requirements/typedefs_neg3.cc: Likewise.
* testsuite/20_util/time_point/cons/81468.cc: New.

From-SVN: r252085

libstdc++-v3/ChangeLog
libstdc++-v3/include/std/chrono
libstdc++-v3/testsuite/20_util/duration/cons/dr1177.cc [new file with mode: 0644]
libstdc++-v3/testsuite/20_util/duration/literals/range.cc
libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg1.cc
libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg2.cc
libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg3.cc
libstdc++-v3/testsuite/20_util/time_point/cons/81468.cc [new file with mode: 0644]

index 0c3ee2a42c89ac70ac00f3db9350370389de9697..36a9744fb4a844959d66456328027e98fb9439d6 100644 (file)
@@ -1,5 +1,27 @@
 2017-09-13  Jonathan Wakely  <jwakely@redhat.com>
 
+       PR libstdc++/81468
+       * include/std/chrono (__enable_if_is_duration)
+       (__disable_if_is_duration): New alias templates to simplify SFINAE.
+       (duration_cast, floor, ceil): Use __enable_if_is_duration.
+       (duration::__is_float, duration::__is_harmonic): New alias templates
+       to simplify SFINAE.
+       (duration::duration(const _Rep2&)): Use _Require, __is_float and
+       __is_harmonic.
+       (duration::duration(const duration<_Rep2, _Period2>&)): Likewise.
+       (__common_rep_type): Remove, replace with ...
+       (__common_rep_t): New alias template.
+       (operator*, operator/, operator%): Use __common_rep_t and
+       __disable_if_is_duration.
+       (time_point::time_point(const time_point<clock, _Dur2>&)): Add missing
+       constraint from LWG DR 1177.
+       * testsuite/20_util/duration/cons/dr1177.cc: New.
+       * testsuite/20_util/duration/literals/range.cc: Update dg-error line.
+       * testsuite/20_util/duration/requirements/typedefs_neg1.cc: Likewise.
+       * testsuite/20_util/duration/requirements/typedefs_neg2.cc: Likewise.
+       * testsuite/20_util/duration/requirements/typedefs_neg3.cc: Likewise.
+       * testsuite/20_util/time_point/cons/81468.cc: New.
+
        * doc/doxygen/mainpage.html: Fix broken URLs.
 
        PR libstdc++/81835
index 1bcbf524a7b6b074a7b2a5dd0c85e7547cd07291..fc058fcd8d8c7982688c5191b176cb9c8206448d 100644 (file)
@@ -179,10 +179,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       : std::true_type
       { };
 
+    template<typename _Tp>
+      using __enable_if_is_duration
+       = typename enable_if<__is_duration<_Tp>::value, _Tp>::type;
+
+    template<typename _Tp>
+      using __disable_if_is_duration
+       = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type;
+
     /// duration_cast
     template<typename _ToDur, typename _Rep, typename _Period>
-      constexpr typename enable_if<__is_duration<_ToDur>::value,
-                                  _ToDur>::type
+      constexpr __enable_if_is_duration<_ToDur>
       duration_cast(const duration<_Rep, _Period>& __d)
       {
        typedef typename _ToDur::period                         __to_period;
@@ -211,7 +218,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 # define __cpp_lib_chrono 201510
 
     template<typename _ToDur, typename _Rep, typename _Period>
-      constexpr enable_if_t<__is_duration<_ToDur>::value, _ToDur>
+      constexpr __enable_if_is_duration<_ToDur>
       floor(const duration<_Rep, _Period>& __d)
       {
        auto __to = chrono::duration_cast<_ToDur>(__d);
@@ -221,7 +228,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       }
 
     template<typename _ToDur, typename _Rep, typename _Period>
-      constexpr enable_if_t<__is_duration<_ToDur>::value, _ToDur>
+      constexpr __enable_if_is_duration<_ToDur>
       ceil(const duration<_Rep, _Period>& __d)
       {
        auto __to = chrono::duration_cast<_ToDur>(__d);
@@ -294,6 +301,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     template<typename _Rep, typename _Period>
       struct duration
       {
+      private:
+       template<typename _Rep2>
+         using __is_float = treat_as_floating_point<_Rep2>;
+
+       // _Period2 is an exact multiple of _Period
+       template<typename _Period2>
+         using __is_harmonic
+           = __bool_constant<ratio_divide<_Period2, _Period>::den == 1>;
+
+      public:
+
        typedef _Rep                                            rep;
        typedef _Period                                         period;
 
@@ -305,22 +323,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        // 20.11.5.1 construction / copy / destroy
        constexpr duration() = default;
 
-       // NB: Make constexpr implicit. This cannot be explicitly
-       // constexpr, as any UDT that is not a literal type with a
-       // constexpr copy constructor will be ill-formed.
        duration(const duration&) = default;
 
-       template<typename _Rep2, typename = typename
-              enable_if<is_convertible<_Rep2, rep>::value
-                        && (treat_as_floating_point<rep>::value
-                            || !treat_as_floating_point<_Rep2>::value)>::type>
+       template<typename _Rep2, typename = _Require<
+                is_convertible<_Rep2, rep>,
+                __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>>
          constexpr explicit duration(const _Rep2& __rep)
          : __r(static_cast<rep>(__rep)) { }
 
-       template<typename _Rep2, typename _Period2, typename = typename
-              enable_if<treat_as_floating_point<rep>::value
-                        || (ratio_divide<_Period2, period>::den == 1
-                            && !treat_as_floating_point<_Rep2>::value)>::type>
+       template<typename _Rep2, typename _Period2, typename = _Require<
+                __or_<__is_float<rep>,
+                      __and_<__is_harmonic<_Period2>,
+                             __not_<__is_float<_Rep2>>>>>>
          constexpr duration(const duration<_Rep2, _Period2>& __d)
          : __r(duration_cast<duration>(__d).count()) { }
 
@@ -455,18 +469,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        return __cd(__cd(__lhs).count() - __cd(__rhs).count());
       }
 
-    template<typename _Rep1, typename _Rep2, bool =
-            is_convertible<_Rep2,
-                           typename common_type<_Rep1, _Rep2>::type>::value>
-      struct __common_rep_type { };
-
-    template<typename _Rep1, typename _Rep2>
-      struct __common_rep_type<_Rep1, _Rep2, true>
-      { typedef typename common_type<_Rep1, _Rep2>::type type; };
+    // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2
+    // is implicitly convertible to it.
+    template<typename _Rep1, typename _Rep2,
+            typename _CRep = typename common_type<_Rep1, _Rep2>::type>
+      using __common_rep_t
+       = typename enable_if<is_convertible<_Rep2, _CRep>::value, _CRep>::type;
 
     template<typename _Rep1, typename _Period, typename _Rep2>
-      constexpr
-      duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period>
+      constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period>
       operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
       {
        typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
@@ -475,14 +486,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       }
 
     template<typename _Rep1, typename _Rep2, typename _Period>
-      constexpr
-      duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period>
+      constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period>
       operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
       { return __d * __s; }
 
     template<typename _Rep1, typename _Period, typename _Rep2>
-      constexpr duration<typename __common_rep_type<_Rep1, typename
-       enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
+      constexpr
+      duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
       operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
       {
        typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
@@ -504,8 +514,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
     // DR 934.
     template<typename _Rep1, typename _Period, typename _Rep2>
-      constexpr duration<typename __common_rep_type<_Rep1, typename
-       enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
+      constexpr
+      duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
       operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
       {
        typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
@@ -614,7 +624,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        { }
 
        // conversions
-       template<typename _Dur2>
+       template<typename _Dur2,
+                typename = _Require<is_convertible<_Dur2, _Dur>>>
          constexpr time_point(const time_point<clock, _Dur2>& __t)
          : __d(__t.time_since_epoch())
          { }
diff --git a/libstdc++-v3/testsuite/20_util/duration/cons/dr1177.cc b/libstdc++-v3/testsuite/20_util/duration/cons/dr1177.cc
new file mode 100644 (file)
index 0000000..28c881c
--- /dev/null
@@ -0,0 +1,41 @@
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do compile { target c++11 } }
+
+#include <chrono>
+#include <type_traits>
+
+using namespace std;
+using namespace std::chrono;
+
+// DR 1177
+static_assert(is_constructible<duration<float>, duration<double>>{},
+    "can convert duration with one floating point rep to another");
+static_assert(is_constructible<duration<float>, duration<int>>{},
+    "can convert duration with integral rep to one with floating point rep");
+static_assert(!is_constructible<duration<int>, duration<float>>{},
+    "cannot convert duration with floating point rep to one with integral rep");
+static_assert(is_constructible<duration<int>, duration<long>>{},
+    "can convert duration with one integral rep to another");
+
+static_assert(!is_constructible<duration<int>, duration<int, ratio<2,3>>>{},
+    "cannot convert duration to one with different period");
+static_assert(is_constructible<duration<float>, duration<int, ratio<2,3>>>{},
+    "unless it has a floating-point representation");
+static_assert(is_constructible<duration<float>, duration<int, ratio<1,3>>>{},
+    "or a period that is an integral multiple of the original");
index b5105dff38bca69bda613d80324b39b36b1444e0..36e71eea72be186492d250943d813b60c112e73e 100644 (file)
@@ -26,6 +26,6 @@ test01()
 
   // std::numeric_limits<int64_t>::max() == 9223372036854775807;
   auto h = 9223372036854775808h;
-  // { dg-error "cannot be represented" "" { target *-*-* } 880 }
+  // { dg-error "cannot be represented" "" { target *-*-* } 891 }
 }
 // { dg-prune-output "in constexpr expansion" } // needed for -O0
index 17f741870984e41066521556baa027db09161db5..d57f6dedd6674c3f86caca1c94f6b27d9c8b093f 100644 (file)
@@ -30,4 +30,4 @@ void test01()
   test_type d; // { dg-error "required from here" }
 }
 
-// { dg-error "rep cannot be a duration" "" { target *-*-* } 300 }
+// { dg-error "rep cannot be a duration" "" { target *-*-* } 318 }
index 282a9dbdc851fe65bd200cc23c2b1205be7db67f..39283b2cb74c6c7af8cb77397b4a1538706d0ac8 100644 (file)
@@ -31,5 +31,5 @@ void test01()
   test_type d;                 // { dg-error "required from here" }
 }
 
-// { dg-error "must be a specialization of ratio" "" { target *-*-* } 301 }
+// { dg-error "must be a specialization of ratio" "" { target *-*-* } 319 }
 // { dg-prune-output "not a member" }
index c57f9d947a541b5d69fed45ce0be48b038b9720d..9beaa83fbfa03f6f2132f63508709bbf99220548 100644 (file)
@@ -32,4 +32,4 @@ void test01()
   test_type d;  // { dg-error "required from here" }
 }
 
-// { dg-error "period must be positive" "" { target *-*-* } 303 }
+// { dg-error "period must be positive" "" { target *-*-* } 321 }
diff --git a/libstdc++-v3/testsuite/20_util/time_point/cons/81468.cc b/libstdc++-v3/testsuite/20_util/time_point/cons/81468.cc
new file mode 100644 (file)
index 0000000..30d1c4a
--- /dev/null
@@ -0,0 +1,34 @@
+// Copyright (C) 2017 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do compile { target c++11 } }
+
+#include <chrono>
+#include <type_traits>
+
+using namespace std;
+using namespace std::chrono;
+
+template <class Duration>
+    using sys_time = time_point<system_clock, Duration>;
+
+static_assert(is_constructible<sys_time<milliseconds>, sys_time<seconds>>{},
+    "Can construct time_point from one with lower precision duration");
+
+// PR libstdc++/81468 - DR 1177
+static_assert(!is_constructible<sys_time<seconds>, sys_time<milliseconds>>{},
+    "Cannot construct time_point from one with higher precision duration");