Is there a way to assign zero to std::chrono::nanoseconds

user3639557 picture user3639557 · May 14, 2015 · Viewed 8.5k times · Source

Is there a way to assign zero to a duration of type std::chrono::nanoseconds? I tried duration::zero but it failed.

Answer

Barry picture Barry · May 14, 2015

There is a zero() function:

std::chrono::nanoseconds dur;
// ...
dur = std::chrono::nanoseconds::zero();

Or you could assign it to a temporary of type nanoseconds explicitly constructed with 0:

dur = std::chrono::nanoseconds{0};

which is what zero() returns too.

Lastly, if you're using a compiler that supports it, there is just:

// requires either "using namespace std::chrono_literals;" or "using namespace std::chrono;"
dur = 0ns;