Commit d0dda804 by Jonathan Wakely Committed by Jonathan Wakely

PR libstdc++/81468 constrain std::chrono::time_point constructor

	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
parent 0f0027d1
2017-09-13 Jonathan Wakely <jwakely@redhat.com> 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. * doc/doxygen/mainpage.html: Fix broken URLs.
PR libstdc++/81835 PR libstdc++/81835
......
...@@ -179,10 +179,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -179,10 +179,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: std::true_type : 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 /// duration_cast
template<typename _ToDur, typename _Rep, typename _Period> template<typename _ToDur, typename _Rep, typename _Period>
constexpr typename enable_if<__is_duration<_ToDur>::value, constexpr __enable_if_is_duration<_ToDur>
_ToDur>::type
duration_cast(const duration<_Rep, _Period>& __d) duration_cast(const duration<_Rep, _Period>& __d)
{ {
typedef typename _ToDur::period __to_period; typedef typename _ToDur::period __to_period;
...@@ -211,7 +218,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -211,7 +218,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
# define __cpp_lib_chrono 201510 # define __cpp_lib_chrono 201510
template<typename _ToDur, typename _Rep, typename _Period> 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) floor(const duration<_Rep, _Period>& __d)
{ {
auto __to = chrono::duration_cast<_ToDur>(__d); auto __to = chrono::duration_cast<_ToDur>(__d);
...@@ -221,7 +228,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -221,7 +228,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
} }
template<typename _ToDur, typename _Rep, typename _Period> 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) ceil(const duration<_Rep, _Period>& __d)
{ {
auto __to = chrono::duration_cast<_ToDur>(__d); auto __to = chrono::duration_cast<_ToDur>(__d);
...@@ -294,6 +301,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -294,6 +301,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _Rep, typename _Period> template<typename _Rep, typename _Period>
struct duration 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 _Rep rep;
typedef _Period period; typedef _Period period;
...@@ -305,22 +323,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -305,22 +323,18 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// 20.11.5.1 construction / copy / destroy // 20.11.5.1 construction / copy / destroy
constexpr duration() = default; 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; duration(const duration&) = default;
template<typename _Rep2, typename = typename template<typename _Rep2, typename = _Require<
enable_if<is_convertible<_Rep2, rep>::value is_convertible<_Rep2, rep>,
&& (treat_as_floating_point<rep>::value __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>>
|| !treat_as_floating_point<_Rep2>::value)>::type>
constexpr explicit duration(const _Rep2& __rep) constexpr explicit duration(const _Rep2& __rep)
: __r(static_cast<rep>(__rep)) { } : __r(static_cast<rep>(__rep)) { }
template<typename _Rep2, typename _Period2, typename = typename template<typename _Rep2, typename _Period2, typename = _Require<
enable_if<treat_as_floating_point<rep>::value __or_<__is_float<rep>,
|| (ratio_divide<_Period2, period>::den == 1 __and_<__is_harmonic<_Period2>,
&& !treat_as_floating_point<_Rep2>::value)>::type> __not_<__is_float<_Rep2>>>>>>
constexpr duration(const duration<_Rep2, _Period2>& __d) constexpr duration(const duration<_Rep2, _Period2>& __d)
: __r(duration_cast<duration>(__d).count()) { } : __r(duration_cast<duration>(__d).count()) { }
...@@ -455,18 +469,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -455,18 +469,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
return __cd(__cd(__lhs).count() - __cd(__rhs).count()); return __cd(__cd(__lhs).count() - __cd(__rhs).count());
} }
template<typename _Rep1, typename _Rep2, bool = // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2
is_convertible<_Rep2, // is implicitly convertible to it.
typename common_type<_Rep1, _Rep2>::type>::value> template<typename _Rep1, typename _Rep2,
struct __common_rep_type { }; typename _CRep = typename common_type<_Rep1, _Rep2>::type>
using __common_rep_t
template<typename _Rep1, typename _Rep2> = typename enable_if<is_convertible<_Rep2, _CRep>::value, _CRep>::type;
struct __common_rep_type<_Rep1, _Rep2, true>
{ typedef typename common_type<_Rep1, _Rep2>::type type; };
template<typename _Rep1, typename _Period, typename _Rep2> template<typename _Rep1, typename _Period, typename _Rep2>
constexpr constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period>
duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period>
operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s) operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
{ {
typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
...@@ -475,14 +486,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -475,14 +486,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
} }
template<typename _Rep1, typename _Rep2, typename _Period> template<typename _Rep1, typename _Rep2, typename _Period>
constexpr constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period>
duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period>
operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
{ return __d * __s; } { return __d * __s; }
template<typename _Rep1, typename _Period, typename _Rep2> template<typename _Rep1, typename _Period, typename _Rep2>
constexpr duration<typename __common_rep_type<_Rep1, typename constexpr
enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period> duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s) operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
{ {
typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
...@@ -504,8 +514,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -504,8 +514,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// DR 934. // DR 934.
template<typename _Rep1, typename _Period, typename _Rep2> template<typename _Rep1, typename _Period, typename _Rep2>
constexpr duration<typename __common_rep_type<_Rep1, typename constexpr
enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period> duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s) operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
{ {
typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
...@@ -614,7 +624,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -614,7 +624,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ } { }
// conversions // conversions
template<typename _Dur2> template<typename _Dur2,
typename = _Require<is_convertible<_Dur2, _Dur>>>
constexpr time_point(const time_point<clock, _Dur2>& __t) constexpr time_point(const time_point<clock, _Dur2>& __t)
: __d(__t.time_since_epoch()) : __d(__t.time_since_epoch())
{ } { }
......
// 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");
...@@ -26,6 +26,6 @@ test01() ...@@ -26,6 +26,6 @@ test01()
// std::numeric_limits<int64_t>::max() == 9223372036854775807; // std::numeric_limits<int64_t>::max() == 9223372036854775807;
auto h = 9223372036854775808h; 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 // { dg-prune-output "in constexpr expansion" } // needed for -O0
...@@ -30,4 +30,4 @@ void test01() ...@@ -30,4 +30,4 @@ void test01()
test_type d; // { dg-error "required from here" } 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 }
...@@ -31,5 +31,5 @@ void test01() ...@@ -31,5 +31,5 @@ void test01()
test_type d; // { dg-error "required from here" } 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" } // { dg-prune-output "not a member" }
...@@ -32,4 +32,4 @@ void test01() ...@@ -32,4 +32,4 @@ void test01()
test_type d; // { dg-error "required from here" } test_type d; // { dg-error "required from here" }
} }
// { dg-error "period must be positive" "" { target *-*-* } 303 } // { dg-error "period must be positive" "" { target *-*-* } 321 }
// 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");
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment