Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative time_point

Tags:

c++

c++11

Is a negative time_point allowed or it is just my implementation that allows this ?

#include <iostream>
#include <chrono>

using namespace std;
using namespace chrono;

int main()
{
    using sc_tp = steady_clock::time_point;
    using sc_dur = steady_clock::duration;
    sc_tp tp( sc_dur( -1 ) );
    cout << tp.time_since_epoch().count() << endl;
}
like image 464
Bonita Montero Avatar asked Oct 16 '25 21:10

Bonita Montero


1 Answers

std::chrono::nanoseconds, std::chrono::microseconds etc. are specified to be stored in a signed integer type but std::chrono::steady_clock only specifies the duration uses an arithmetic type. Every implementation I've seen uses one of the chrono helper duration types for steady_clock::duration but there is nothing in the standard requiring that to be the case so it could use an unsigned duration.

Note (thanks to Howard Hinnant) that std::chrono::system_clock does specify that the duration uses a signed type.

like image 87
Alan Birtles Avatar answered Oct 18 '25 10:10

Alan Birtles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!