如何重置high_resolution_clock :: time_point

ida*_*hmu 2 c++ c++11 c++-chrono

我正在开发一个类Timer,其中一些成员的类型为 high_resolution_clock :: time_point,其中time_point定义为typedef chrono::time_point<system_clock> time_point;

这个对象的默认值是多少?

我需要从以下几个方面了解这个价值:

  1. 知道成员是否已初始化
  2. 实现Timer::Reset()功能

背景

class Timer
{
    void Start() { m_tpStop = high_resolution_clock::now(); }
    void Stop() { m_tpStart = high_resolution_clock::now(); }

    bool WasStarted() { /* TO-DO */ }

    void Reset();
    __int64 GetDuration();

    high_resolution_clock::time_point m_tpStart;
    high_resolution_clock::time_point m_tpStop;
};
Run Code Online (Sandbox Code Playgroud)

那么,我可以Timer::WasStarted通过仅查看成员来实现m_tpStart吗?我想不要为此目的添加布尔成员.

eer*_*ika 8

那么,我可以通过仅查看成员m_tpStart来实现Timer :: WasStarted吗?

好吧,如果你定义了这样的不变量,那m_tpStart就是零(纪元)当且仅当计时器被重置(未启动)时,它才是微不足道的.只需检查start是否为epoch,以测试计时器是否已启动.

究竟如何设置一个时间点到epoch,似乎有点复杂 - 我想这就是你所说的" 如何重置high_resolution_clock :: time_point ".您需要复制 - 分配默认构造的时间点.

void Start() { m_tpStart = high_resolution_clock::now(); }
void Stop() {
    m_tpStop = high_resolution_clock::now();
}
bool WasStarted() {
    return m_tpStart.time_since_epoch().count(); // unit doesn't matter
}
void Reset() {
    m_tpStart = m_tpStop = {};
}
Run Code Online (Sandbox Code Playgroud)