Search code examples
c++c++-chrono

How initialize time points and set them to a falsy value afterward in C++?


I am new to C++ and I want to implement the following behavior.

#include <thread>
#include <chrono>
#include <iostream>

using namespace std;

int main(){
    chrono::high_resolution_clock::time_point start_time_point;
    while(true){
      if(statement_1){
         // check whether start_time_point is falsy
         if(...) start_time_point = chrono::high_resolution_clock::now();
         auto current_time_point = chrono::high_resolution_clock::now();
         if(chrono::duration_cast<std::chrono::seconds>(current_time_point - start_time_point).count() > 5) {
            // do something
         }
      }

      if(statement_2){
         // assign some falsy value to start_time_point
         // so that next time the first if block is entered a new time point is assigned to start_time_point
      }
   }

}

I am used to javascript, so in JS I only would assign null to start_time_point in the second if block but in C++ it is different. How would I achieve the above-mentioned behavior?


Solution

  • I am used to javascript, so in JS I only would assign null to start_time_point in the second if block but in C++ it is different. How would I achieve the above-mentioned behavior?

    A chrono::time_point has a min() member, returning the lowest possible time point. It is not identical, but could be used similar to the null value in javascript.

    So perhaps

    start_time_point = start_time_point.min();