[PATCH] Simplify overflow checks in duration literals

Jonathan Wakely jwakely@redhat.com
Tue Aug 14 13:55:00 GMT 2018


	* include/std/chrono (__check_overflow): Simplify definition.
	(_Checked_integral_constant): Remove.

Tested x86_64-linux, committed to trunk.


-------------- next part --------------
commit c0dec9d05de4695136694f78f2f76e3c9f15b3f1
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Aug 14 14:33:38 2018 +0100

    Simplify overflow checks in duration literals
    
            * include/std/chrono (__check_overflow): Simplify definition.
            (_Checked_integral_constant): Remove.

diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono
index da03fdccce4..871c896144a 100644
--- a/libstdc++-v3/include/std/chrono
+++ b/libstdc++-v3/include/std/chrono
@@ -900,24 +900,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   {
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wliteral-suffix"
-    template<typename _Rep, unsigned long long _Val>
-      struct _Checked_integral_constant
-      : integral_constant<_Rep, static_cast<_Rep>(_Val)>
-      {
-	static_assert(_Checked_integral_constant::value >= 0
-		      && _Checked_integral_constant::value == _Val,
-		      "literal value cannot be represented by duration type");
-      };
-
     template<typename _Dur, char... _Digits>
       constexpr _Dur __check_overflow()
       {
 	using _Val = __parse_int::_Parse_int<_Digits...>;
-	using _Rep = typename _Dur::rep;
-	// TODO: should be simply integral_constant<_Rep, _Val::value>
-	// but GCC doesn't reject narrowing conversions to _Rep.
-	using _CheckedVal = _Checked_integral_constant<_Rep, _Val::value>;
-	return _Dur{_CheckedVal::value};
+	constexpr typename _Dur::rep __repval = _Val::value;
+	static_assert(__repval >= 0 && __repval == _Val::value,
+		      "literal value cannot be represented by duration type");
+	return _Dur(__repval);
       }
 
     constexpr chrono::duration<long double, ratio<3600,1>>


More information about the Libstdc++ mailing list