This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[v3] Implement resolution of DR 1177
- From: Paolo Carlini <paolo dot carlini at oracle dot com>
- To: "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>
- Cc: libstdc++ <libstdc++ at gcc dot gnu dot org>
- Date: Sun, 01 Nov 2009 01:36:55 +0100
- Subject: [v3] Implement resolution of DR 1177
Hi,
tested x86_64-linux, committed to mainline.
Paolo.
///////////////////////
2009-10-31 Paolo Carlini <paolo.carlini@oracle.com>
* include/std/chrono (struct __common_rep_type): New.
(duration_cast(const duration<>&),
duration<>::duration(const _Rep2&),
duration<>::duration(const duration<>&),
operator*(const duration<>&, const _Rep2&),
operator*(const _Rep1&, const duration<>&),
operator/(const duration<>&, const _Rep2&),
time_point_cast(const time_point<>&)): Implement resolution of
DR 1177 ([Ready] in Santa Cruz), change to not participate to
overload resolution if the constraints are not met.
* testsuite/20_util/duration/cons/1_neg.cc: Adjust dg-errors.
* testsuite/20_util/duration/cons/dr974.cc: Likewise.
* testsuite/20_util/duration/requirements/typedefs_neg1.cc: Adjust
dg-error line numbers.
* testsuite/20_util/duration/requirements/typedefs_neg2.cc: Likewise.
* testsuite/20_util/duration/requirements/typedefs_neg3.cc: Likewise.
Index: include/std/chrono
===================================================================
--- include/std/chrono (revision 153781)
+++ include/std/chrono (working copy)
@@ -138,9 +138,20 @@
}
};
+ template<typename _Tp>
+ struct __is_duration
+ : std::false_type
+ { };
+
+ template<typename _Rep, typename _Period>
+ struct __is_duration<duration<_Rep, _Period>>
+ : std::true_type
+ { };
+
/// duration_cast
template<typename _ToDuration, typename _Rep, typename _Period>
- inline _ToDuration
+ inline typename enable_if<__is_duration<_ToDuration>::value,
+ _ToDuration>::type
duration_cast(const duration<_Rep, _Period>& __d)
{
typedef typename
@@ -175,16 +186,6 @@
{ return numeric_limits<_Rep>::min(); }
};
- template<typename _Tp>
- struct __is_duration
- : std::false_type
- { };
-
- template<typename _Rep, typename _Period>
- struct __is_duration<duration<_Rep, _Period>>
- : std::true_type
- { };
-
template<typename T>
struct __is_ratio
: std::false_type
@@ -210,25 +211,19 @@
// 20.8.3.1 construction / copy / destroy
duration() = default;
- template<typename _Rep2>
+ 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>
explicit duration(const _Rep2& __rep)
- : __r(static_cast<rep>(__rep))
- {
- static_assert(is_convertible<_Rep2,rep>::value
- && (treat_as_floating_point<rep>::value
- || !treat_as_floating_point<_Rep2>::value),
- "cannot construct integral duration with floating point type");
- }
+ : __r(static_cast<rep>(__rep)) { }
- template<typename _Rep2, typename _Period2>
+ template<typename _Rep2, typename _Period2, typename = typename
+ enable_if<treat_as_floating_point<rep>::value
+ || (ratio_divide<_Period2, period>::type::den == 1
+ && !treat_as_floating_point<_Rep2>::value)>::type>
duration(const duration<_Rep2, _Period2>& __d)
- : __r(duration_cast<duration>(__d).count())
- {
- static_assert(treat_as_floating_point<rep>::value == true
- || (ratio_divide<_Period2, period>::type::den == 1
- && !treat_as_floating_point<_Rep2>::value),
- "the resulting duration is not exactly representable");
- }
+ : __r(duration_cast<duration>(__d).count()) { }
~duration() = default;
duration(const duration&) = default;
@@ -359,8 +354,17 @@
return __ct(__lhs) -= __rhs;
}
+ 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; };
+
template<typename _Rep1, typename _Period, typename _Rep2>
- inline duration<typename common_type<_Rep1, _Rep2>::type, _Period>
+ inline duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period>
operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
{
typedef typename common_type<_Rep1, _Rep2>::type __cr;
@@ -368,12 +372,12 @@
}
template<typename _Rep1, typename _Period, typename _Rep2>
- inline duration<typename common_type<_Rep1, _Rep2>::type, _Period>
- operator*(const _Rep2& __s, const duration<_Rep1, _Period>& __d)
+ inline duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period>
+ operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
{ return __d * __s; }
template<typename _Rep1, typename _Period, typename _Rep2>
- inline duration<typename common_type<_Rep1, typename
+ inline duration<typename __common_rep_type<_Rep1, typename
enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
{
@@ -394,7 +398,7 @@
// DR 934.
template<typename _Rep1, typename _Period, typename _Rep2>
- inline duration<typename common_type<_Rep1, typename
+ inline duration<typename __common_rep_type<_Rep1, typename
enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
{
@@ -541,7 +545,8 @@
/// time_point_cast
template<typename _ToDuration, typename _Clock, typename _Duration>
- inline time_point<_Clock, _ToDuration>
+ inline typename enable_if<__is_duration<_ToDuration>::value,
+ time_point<_Clock, _ToDuration>>::type
time_point_cast(const time_point<_Clock, _Duration>& __t)
{
return time_point<_Clock, _ToDuration>(
Index: testsuite/20_util/duration/cons/1_neg.cc
===================================================================
--- testsuite/20_util/duration/cons/1_neg.cc (revision 153781)
+++ testsuite/20_util/duration/cons/1_neg.cc (working copy)
@@ -26,7 +26,7 @@
void
test01()
{
- std::chrono::duration<int> d1(1.0);
+ std::chrono::duration<int> d1(1.0); // { dg-error "no matching" }
}
void
@@ -35,11 +35,7 @@
using namespace std::chrono;
duration<int, std::micro> d2(8);
- duration<int, std::milli> d2_copy(d2);
+ duration<int, std::milli> d2_copy(d2); // { dg-error "no matching" }
}
-// { dg-error "instantiated from here" "" { target *-*-* } 29 }
-// { dg-error "instantiated from here" "" { target *-*-* } 38 }
-// { dg-error "not exactly representable" "" { target *-*-* } 227 }
-// { dg-error "integral duration with floating point" "" { target *-*-* } 217 }
-// { dg-excess-errors "In instantiation of" }
+// { dg-excess-errors "candidates are" }
Index: testsuite/20_util/duration/cons/dr974.cc
===================================================================
--- testsuite/20_util/duration/cons/dr974.cc (revision 153781)
+++ testsuite/20_util/duration/cons/dr974.cc (working copy)
@@ -29,9 +29,5 @@
using namespace std::chrono;
duration<double> d(3.5);
- duration<int> i = d; // implicit truncation, should not compile
+ duration<int> i = d; // { dg-error "conversion" }
}
-
-// { dg-error "instantiated from here" "" { target *-*-* } 32 }
-// { dg-error "not exactly representable" "" { target *-*-* } 227 }
-// { dg-excess-errors "In instantiation of" }
Index: testsuite/20_util/duration/requirements/typedefs_neg1.cc
===================================================================
--- testsuite/20_util/duration/requirements/typedefs_neg1.cc (revision 153781)
+++ testsuite/20_util/duration/requirements/typedefs_neg1.cc (working copy)
@@ -31,6 +31,6 @@
test_type d;
}
-// { dg-error "rep cannot be a duration" "" { target *-*-* } 202 }
+// { dg-error "rep cannot be a duration" "" { target *-*-* } 203 }
// { dg-error "instantiated from here" "" { target *-*-* } 31 }
// { dg-excess-errors "In instantiation of" }
Index: testsuite/20_util/duration/requirements/typedefs_neg2.cc
===================================================================
--- testsuite/20_util/duration/requirements/typedefs_neg2.cc (revision 153781)
+++ testsuite/20_util/duration/requirements/typedefs_neg2.cc (working copy)
@@ -32,6 +32,6 @@
test_type d;
}
-// { dg-error "must be a specialization of ratio" "" { target *-*-* } 203 }
+// { dg-error "must be a specialization of ratio" "" { target *-*-* } 204 }
// { dg-error "instantiated from here" "" { target *-*-* } 32 }
// { dg-excess-errors "In instantiation of" }
Index: testsuite/20_util/duration/requirements/typedefs_neg3.cc
===================================================================
--- testsuite/20_util/duration/requirements/typedefs_neg3.cc (revision 153781)
+++ testsuite/20_util/duration/requirements/typedefs_neg3.cc (working copy)
@@ -33,6 +33,6 @@
test_type d;
}
-// { dg-error "period must be positive" "" { target *-*-* } 205 }
+// { dg-error "period must be positive" "" { target *-*-* } 206 }
// { dg-error "instantiated from here" "" { target *-*-* } 33 }
// { dg-excess-errors "In instantiation of" }