Patchwork [v3] libstdc++/49151

login
register
mail settings
Submitter Paolo Carlini
Date May 24, 2011, 11:34 p.m.
Message ID <4DDC4070.7000200@oracle.com>
Download mbox | patch
Permalink /patch/97244/
State New
Headers show

Comments

Paolo Carlini - May 24, 2011, 11:34 p.m.
Hi,

straightforward implementation of the resolution + some missing 
constexpr specifiers.

Tested x86_64-linux, committed.

Paolo.

////////////////////
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.

Patch

Index: include/std/chrono
===================================================================
--- include/std/chrono	(revision 174124)
+++ include/std/chrono	(working copy)
@@ -352,28 +352,28 @@ 
 
     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 @@ 
       { 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
Index: testsuite/20_util/duration/arithmetic/dr2020.cc
===================================================================
--- testsuite/20_util/duration/arithmetic/dr2020.cc	(revision 0)
+++ testsuite/20_util/duration/arithmetic/dr2020.cc	(revision 0)
@@ -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;
+}