diff options
author | Paolo Carlini <paolo.carlini@oracle.com> | 2011-05-24 23:29:19 +0000 |
---|---|---|
committer | Paolo Carlini <paolo@gcc.gnu.org> | 2011-05-24 23:29:19 +0000 |
commit | b850be9d50210da752443e730723285bbd1b7c96 (patch) | |
tree | c4d004bbe77dcf9366cdceb284257616b3ae489d | |
parent | 24d1bbc7b5ea8655be7fa40a462a417cac8e8ecd (diff) | |
download | gcc-b850be9d50210da752443e730723285bbd1b7c96.zip gcc-b850be9d50210da752443e730723285bbd1b7c96.tar.gz gcc-b850be9d50210da752443e730723285bbd1b7c96.tar.bz2 |
re PR libstdc++/49151 ([C++0x][constexpr] chrono::duration has incorrect non-member operator semantics)
2011-05-24 Paolo Carlini <paolo.carlini@oracle.com>
PR libstdc++/49151
* include/std/chrono (operator+, operator-, operator*, operator/,
operator&): Implement LWG 2020 [WP]; specify constexpr.
* testsuite/20_util/duration/arithmetic/dr2020.cc: New.
From-SVN: r174150
-rw-r--r-- | libstdc++-v3/ChangeLog | 7 | ||||
-rw-r--r-- | libstdc++-v3/include/std/chrono | 55 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/20_util/duration/arithmetic/dr2020.cc | 66 |
3 files changed, 103 insertions, 25 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index ff39d07..3480c8a 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,5 +1,12 @@ 2011-05-24 Paolo Carlini <paolo.carlini@oracle.com> + PR libstdc++/49151 + * include/std/chrono (operator+, operator-, operator*, operator/, + operator&): Implement LWG 2020 [WP]; specify constexpr. + * testsuite/20_util/duration/arithmetic/dr2020.cc: New. + +2011-05-24 Paolo Carlini <paolo.carlini@oracle.com> + PR libstdc++/49141 * testsuite/26_numerics/complex/cons/48760.cc: Use dg-require-c-std. * testsuite/26_numerics/complex/cons/48760_c++0x.cc: Likewise. diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono index 89ded2c..86e6e00 100644 --- a/libstdc++-v3/include/std/chrono +++ b/libstdc++-v3/include/std/chrono @@ -352,28 +352,28 @@ _GLIBCXX_END_NAMESPACE_VERSION template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2> - inline typename common_type<duration<_Rep1, _Period1>, - duration<_Rep2, _Period2>>::type + inline constexpr typename common_type<duration<_Rep1, _Period1>, + duration<_Rep2, _Period2>>::type operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) { typedef duration<_Rep1, _Period1> __dur1; typedef duration<_Rep2, _Period2> __dur2; - typedef typename common_type<__dur1,__dur2>::type __ct; - return __ct(__lhs) += __rhs; + typedef typename common_type<__dur1,__dur2>::type __cd; + return __cd(__cd(__lhs).count() + __cd(__rhs).count()); } template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2> - inline typename common_type<duration<_Rep1, _Period1>, - duration<_Rep2, _Period2>>::type + inline constexpr typename common_type<duration<_Rep1, _Period1>, + duration<_Rep2, _Period2>>::type operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) { typedef duration<_Rep1, _Period1> __dur1; typedef duration<_Rep2, _Period2> __dur2; - typedef typename common_type<__dur1,__dur2>::type __ct; - return __ct(__lhs) -= __rhs; + typedef typename common_type<__dur1,__dur2>::type __cd; + return __cd(__cd(__lhs).count() - __cd(__rhs).count()); } template<typename _Rep1, typename _Rep2, bool = @@ -386,60 +386,65 @@ _GLIBCXX_END_NAMESPACE_VERSION { typedef typename common_type<_Rep1, _Rep2>::type type; }; template<typename _Rep1, typename _Period, typename _Rep2> - inline duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period> + inline constexpr + 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; - return duration<__cr, _Period>(__d) *= __s; + typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> + __cd; + return __cd(__cd(__d).count() * __s); } template<typename _Rep1, typename _Period, typename _Rep2> - inline duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period> + inline constexpr + 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_rep_type<_Rep1, typename + inline constexpr 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) { - typedef typename common_type<_Rep1, _Rep2>::type __cr; - return duration<__cr, _Period>(__d) /= __s; + typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> + __cd; + return __cd(__cd(__d).count() / __s); } template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2> - inline typename common_type<_Rep1, _Rep2>::type + inline constexpr typename common_type<_Rep1, _Rep2>::type operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) { typedef duration<_Rep1, _Period1> __dur1; typedef duration<_Rep2, _Period2> __dur2; - typedef typename common_type<__dur1,__dur2>::type __ct; - return __ct(__lhs).count() / __ct(__rhs).count(); + typedef typename common_type<__dur1,__dur2>::type __cd; + return __cd(__lhs).count() / __cd(__rhs).count(); } // DR 934. template<typename _Rep1, typename _Period, typename _Rep2> - inline duration<typename __common_rep_type<_Rep1, typename + inline constexpr 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) { - typedef typename common_type<_Rep1, _Rep2>::type __cr; - return duration<__cr, _Period>(__d) %= __s; + typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> + __cd; + return __cd(__cd(__d).count() % __s); } template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2> - inline typename common_type<duration<_Rep1, _Period1>, - duration<_Rep2, _Period2>>::type + inline constexpr typename common_type<duration<_Rep1, _Period1>, + duration<_Rep2, _Period2>>::type operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) { typedef duration<_Rep1, _Period1> __dur1; typedef duration<_Rep2, _Period2> __dur2; - typedef typename common_type<__dur1,__dur2>::type __ct; - return __ct(__lhs) %= __rhs; + typedef typename common_type<__dur1,__dur2>::type __cd; + return __cd(__cd(__lhs).count() % __cd(__rhs).count()); } // comparisons diff --git a/libstdc++-v3/testsuite/20_util/duration/arithmetic/dr2020.cc b/libstdc++-v3/testsuite/20_util/duration/arithmetic/dr2020.cc new file mode 100644 index 0000000..29945ae --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/duration/arithmetic/dr2020.cc @@ -0,0 +1,66 @@ +// { dg-options "-std=gnu++0x" } +// { dg-require-cstdint "" } + +// Copyright (C) 2011 Free Software Foundation +// +// 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/>. + +// 20.11.5 Class template duration [time.duration] + +#include <chrono> +#include <testsuite_hooks.h> + +// DR 2020 +void test01() +{ + bool test __attribute__((unused)) = true; + using namespace std::chrono; + + constexpr duration<int> d0(12); + constexpr duration<int> d1(3); + constexpr int i = 5; + + constexpr auto d2 = d0 + d1; + VERIFY( d2.count() == 15 ); + + constexpr auto d3 = d0 - d1; + VERIFY( d3.count() == 9 ); + + constexpr auto d4 = d0 * 5; + VERIFY( d4.count() == 60 ); + + constexpr auto d5 = i * d0; + VERIFY( d5.count() == 60 ); + + constexpr auto d6 = d0 % i; + VERIFY( d6.count() == 2 ); + + constexpr auto j = d0 % d1; + VERIFY( j.count() == 0 ); + + constexpr auto d7 = d0 / i; + VERIFY( d7.count() == 2 ); + + constexpr auto k = d0 / d1; + VERIFY( k == 4 ); +} + +int +main() +{ + test01(); + return 0; +} |