user3639557 user3639557 -4 years ago 214
C++ Question

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

Is there a way to assign zero to a duration of type

? I tried
but it failed.

Answer Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download