I want to be able to get nanosecond accuracy with the chrono library but I can't figure out how to convert std::chrono::high_resolution_clock::now()
into long int
. I tried this:
#include <chrono>
#include <iostream>
using namespace std;
int main() {
typedef std::chrono::high_resolution_clock Clock;
long int val = Clock::now();
cout << val << endl;
cin.ignore();
return 0;
}
But this gave me the error: error C2440: 'initializing' : cannot convert from 'std::chrono::system_clock::time_point' to 'long'
How can I convert it to a 64 bit int? If I can't then I don't see how chrono is useful.
now
function returns atime_point
object (just like the error message says). See thetime_point
link for an example on how to print the time. If you want the time in seconds, use e.g.to_time_t
.<chrono>
s advantages is that it is type safe; two examples are that time points and time durations are distinct, non-interchangeable types and the types keep track of units for you, so you can't confuse seconds for milliseconds, etc.