OSDN Git Service

2011-05-24 Paolo Carlini <paolo.carlini@oracle.com>
authorpaolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 24 May 2011 23:29:19 +0000 (23:29 +0000)
committerpaolo <paolo@138bc75d-0d04-0410-961f-82ee72b054a4>
Tue, 24 May 2011 23:29:19 +0000 (23:29 +0000)
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.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@174150 138bc75d-0d04-0410-961f-82ee72b054a4

libstdc++-v3/ChangeLog
libstdc++-v3/include/std/chrono
libstdc++-v3/testsuite/20_util/duration/arithmetic/dr2020.cc [new file with mode: 0644]

index ff39d07..3480c8a 100644 (file)
@@ -1,5 +1,12 @@
 2011-05-24  Paolo Carlini  <paolo.carlini@oracle.com>
 
 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.
        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.
index 89ded2c..86e6e00 100644 (file)
@@ -352,28 +352,28 @@ _GLIBCXX_END_NAMESPACE_VERSION
 
     template<typename _Rep1, typename _Period1,
             typename _Rep2, typename _Period2>
 
     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;
       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>
       }
 
     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;
       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 =
       }
 
     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>
       { 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)
       {
       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>
       }
 
     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>
       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)
       {
        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>
       }
 
      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;
       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>
       }
 
     // 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)
       {
        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>
       }
 
      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;
       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
       }
 
     // 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 (file)
index 0000000..29945ae
--- /dev/null
@@ -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;
+}