Question Details

No question body available.

Tags

c++ c++-chrono

Answers (1)

April 20, 2026 Score: 5 Rep: 226,185 Quality: Medium Completeness: 100%

Is there a way to easily automate this checking in modern C++, at least for debug builds?

Yes.

The durations (and time_points) can use class types for the underlying representation as opposed to integral (or floating point) types. For example they can use a "safe int" type that turns overflow into a noisy run-time error.

One free, open-source, header-only example of such a class is the bbi integer library.1 This library can be used to easily define your own custom chrono types that will terminate on overflow.

For example:

#include "bbi.h"
#include 
#include 

namespace mychrono {

using namespace bbi::term;

using nanoseconds = std::chrono::duration; using microseconds = std::chrono::duration; using milliseconds = std::chrono::duration; using seconds = std::chrono::duration; using minutes = std::chrono::duration; using hours = std::chrono::duration>; using weeks = std::chrono::duration