From b850be9d50210da752443e730723285bbd1b7c96 Mon Sep 17 00:00:00 2001 From: Paolo Carlini Date: Tue, 24 May 2011 23:29:19 +0000 Subject: [PATCH] re PR libstdc++/49151 ([C++0x][constexpr] chrono::duration has incorrect non-member operator semantics) 2011-05-24 Paolo Carlini 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 --- libstdc++-v3/ChangeLog | 7 ++ libstdc++-v3/include/std/chrono | 55 +++++++++------- .../20_util/duration/arithmetic/dr2020.cc | 66 +++++++++++++++++++ 3 files changed, 103 insertions(+), 25 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/duration/arithmetic/dr2020.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index ff39d0796a0..3480c8ab7df 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +2011-05-24 Paolo Carlini + + 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 PR libstdc++/49141 diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono index 89ded2c80d4..86e6e0019e1 100644 --- a/libstdc++-v3/include/std/chrono +++ b/libstdc++-v3/include/std/chrono @@ -352,28 +352,28 @@ _GLIBCXX_END_NAMESPACE_VERSION template - inline typename common_type, - duration<_Rep2, _Period2>>::type + inline constexpr typename common_type, + 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 - inline typename common_type, - duration<_Rep2, _Period2>>::type + inline constexpr typename common_type, + 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::type type; }; template - inline duration::type, _Period> + inline constexpr + duration::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::type, _Period> + __cd; + return __cd(__cd(__d).count() * __s); } template - inline duration::type, _Period> + inline constexpr + duration::type, _Period> operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) { return __d * __s; } template - inline duration::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::type, _Period> + __cd; + return __cd(__cd(__d).count() / __s); } template - 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 - inline duration::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::type, _Period> + __cd; + return __cd(__cd(__d).count() % __s); } template - inline typename common_type, - duration<_Rep2, _Period2>>::type + inline constexpr typename common_type, + 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 00000000000..29945aeb764 --- /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 +// . + +// 20.11.5 Class template duration [time.duration] + +#include +#include + +// DR 2020 +void test01() +{ + bool test __attribute__((unused)) = true; + using namespace std::chrono; + + constexpr duration d0(12); + constexpr duration 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; +} -- 2.30.2