Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ chrono::zero() Function



The std::chrono::zero() function in C++, return a time point or duration object that represents the zero value for a given time unit, such as seconds, milliseconds or microseconds. It is also used for initializing time related variables or resetting timers to a baseline.

This function is defined for various time units including chrono::seconds, chrono::milliseconds,and helps to ensure that time related calculations are performed with a known starting point.

Syntax

Following is the syntax for std::chrono::zero() function.

static constexpr duration zero();
or
static constexpr duration zero() noexcept;

Parameters

This function does not accepts any parameters.

Return value

This function returns the result of the operation.

The std::chrono::zero is not a valid function or member in C++ standard library, hence we initialize a zero-duration object directly using the chrono::seconds(0).

Example 1

In the following example, we are going to consider the basic usage of the zero() function.

#include <iostream>
#include <chrono>
int main() {
   std::chrono::seconds a(0);
   std::cout << "Result: " << a.count() << " seconds" << std::endl;
   return 0;
}

Output

Output of the above code is as follows −

Result: 0 seconds

Example 2

Consider the following example, we are going to use the chrono::zero() to check if a duration is zero or not.

#include <iostream>
#include <chrono>
int main() {
   std::chrono::seconds a(0);
   std::chrono::seconds zero_duration(0);
   if (a == zero_duration) {
      std::cout << "Duration is zero." << std::endl;
   } else {
      std::cout << "Duration is not zero." << std::endl;
   }
   return 0;
}

Output

Following is the output of the above code −

Duration is zero.
cpp_chrono.htm
Advertisements