This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[patch] fix std::chrono::duration literals
- From: Jonathan Wakely <jwakely at redhat dot com>
- To: libstdc++ at gcc dot gnu dot org, gcc-patches at gcc dot gnu dot org
- Date: Mon, 23 Jun 2014 12:32:05 +0100
- Subject: [patch] fix std::chrono::duration literals
- Authentication-results: sourceware.org; auth=none
This fixes errors when using 0s or 1'000s to create durations.
Tested x86_64-linux, committed to trunk.
commit c57e645222ac9bf476ef5b6414773b5f4e2b86fc
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Sun Jun 22 18:06:42 2014 +0100
* include/bits/parse_numbers.h (_Number_help): Fix divide-by-zero.
* include/std/chrono (_Checked_integral_constant): Allow zero.
* testsuite/20_util/duration/literals/values.cc: Test non-positive
values and digit separators.
diff --git a/libstdc++-v3/include/bits/parse_numbers.h b/libstdc++-v3/include/bits/parse_numbers.h
index a29d127..f46c59c 100644
--- a/libstdc++-v3/include/bits/parse_numbers.h
+++ b/libstdc++-v3/include/bits/parse_numbers.h
@@ -190,10 +190,11 @@ namespace __parse_int
using __digit = _Digit<_Base, _Dig>;
using __valid_digit = typename __digit::__valid;
using __next = _Number_help<_Base,
- _Pow / (_Base * __valid_digit::value),
+ __valid_digit::value ? _Pow / _Base : _Pow,
_Digs...>;
using type = __ull_constant<_Pow * __digit::value + __next::type::value>;
- static_assert((type::value / _Pow) == __digit::value, "overflow");
+ static_assert((type::value / _Pow) == __digit::value,
+ "integer literal does not fit in unsigned long long");
};
template<unsigned _Base, unsigned long long _Pow, char _Dig>
@@ -214,7 +215,6 @@ namespace __parse_int
{ };
//------------------------------------------------------------------------------
-// This _Parse_int is the same 'level' as the old _Base_dispatch.
template<char... _Digs>
struct _Parse_int;
diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono
index 39ad5e3..88eaa16 100644
--- a/libstdc++-v3/include/std/chrono
+++ b/libstdc++-v3/include/std/chrono
@@ -791,7 +791,7 @@ _GLIBCXX_END_NAMESPACE_VERSION
struct _Checked_integral_constant
: integral_constant<_Rep, static_cast<_Rep>(_Val)>
{
- static_assert(_Checked_integral_constant::value > 0
+ static_assert(_Checked_integral_constant::value >= 0
&& _Checked_integral_constant::value == _Val,
"literal value cannot be represented by duration type");
};
diff --git a/libstdc++-v3/testsuite/20_util/duration/literals/values.cc b/libstdc++-v3/testsuite/20_util/duration/literals/values.cc
index f55c32f..ce86358 100644
--- a/libstdc++-v3/testsuite/20_util/duration/literals/values.cc
+++ b/libstdc++-v3/testsuite/20_util/duration/literals/values.cc
@@ -56,6 +56,12 @@ test03()
VERIFY( workday == std::chrono::hours(8) );
auto fworkday = 8.0h;
VERIFY( (fworkday == std::chrono::duration<long double, std::ratio<3600,1>>(8.0L)) );
+ auto immediate = 0s;
+ VERIFY( immediate == std::chrono::seconds(0) );
+ auto minute_ago = -1min;
+ VERIFY( minute_ago == std::chrono::minutes(-1) );
+ auto separated = 1'000'000s;
+ VERIFY( separated == std::chrono::seconds(1'000'000) );
}
int