Álvaro Pérez
Álvaro Pérez

Reputation: 331

g++ std::chrono assertion break

I'm having trouble with some code from a C++ project. It includes the std::chrono library and keeps breaking at the following assertion:

static_assert(system_clock::duration::min() < system_clock::duration::zero(), "a clock's minimum duration cannot be less than its epoch");

The assert breaks the code in both a Debian machine with g++ 6.3.0 and in a PC with Windows 10, CygWin and g++ 7.3.0. I've also tried in an online C++ compiler a simple example including the chrono library, which by itself does not give any problems, but when comparing manually the minimum and zero duration of the chrono system clock gives the result that should trigger the assert as well.

I've searched about the issue and found some clues leading to some related problems caused by the TZ posix variable that holds timezone info. Tried unsetting and setting it to its right value, yet it had no effects on the assert.

I'd appreciate any pointers or suggestions.

Edit: While std::chrono::milliseconds::zero() has a (as expected) value of 0, the value of std::chrono::milliseconds::min() is -9223372036854775808, or -2^63 which i think is the minimum possible value for a long long value (possible overflow?).

Upvotes: 0

Views: 645

Answers (3)

lisuwang
lisuwang

Reputation: 18

I maybe have two suggestions :

  1. In general , assert fail only happen in Debug version , so if you just want to build successfully, you can build to release version to avoid the problem.
  2. You can confirm your Debian and Windows time zone in proper zone .

Upvotes: 0

&#193;lvaro P&#233;rez
&#193;lvaro P&#233;rez

Reputation: 331

After some tests i realized the assert was being triggered in both systems only when using the g++ through the testing software being used, since the same code compiled outside it did not fail the assertion with the same compilers.

It turns out that the software uses the EDG parser and it needs the option --64_bit_target to avoid triggering the assert. Unfortunately, no information about the option exists in the parser documentation, so i can't know the reason why this issue happens without it.

Probably the question doesn't have much value now, but i didn't want to delete it since people already wrote answers that may be of interest to someone.

Upvotes: 1

T Percival
T Percival

Reputation: 8684

A duration can be negative, as you found with the highly negative value of …::min(). The assertion is incorrect, almost like asserting that -1 must be greater than zero.

The C++17 spec declares an abs() function for finding an absolute duration, and discusses its applicability with signed and unsigned representations:

23.17.5.9 duration algorithms [time.duration.alg]

template <class Rep, class Period> constexpr duration<Rep, Period> abs(duration<Rep, Period> d);

1 Remarks: This function shall not participate in overload resolution unless numeric_limits<Rep>::is_signed is true.

2 Returns: If d >= d.zero(), return d, otherwise return -d.

Upvotes: 0

Related Questions