如何重置 high_resolution_clock::time_point
How to reset the high_resolution_clock::time_point
我正在开发一个 class Timer
,它的一些成员属于 high_resolution_clock::time_point 类型,其中 time_point
定义为 typedef chrono::time_point<system_clock> time_point;
问题
这个对象的默认值是多少?
出于以下几个原因,我需要了解此值:
- 知道成员是否被初始化
- 实现
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;
};
那么,我可以通过只查看成员 m_tpStart
来实现 Timer::WasStarted
吗?我不想为此添加布尔值成员。
So, can I implement Timer::WasStarted by looking only at member m_tpStart?
好吧,如果你定义这样的不变量,当且仅当计时器被重置(未启动)时,m_tpStart
为零(纪元),那么它是微不足道的。只需检查start是否为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 = {};
}
我正在开发一个 class Timer
,它的一些成员属于 high_resolution_clock::time_point 类型,其中 time_point
定义为 typedef chrono::time_point<system_clock> time_point;
问题
这个对象的默认值是多少?
出于以下几个原因,我需要了解此值:
- 知道成员是否被初始化
- 实现
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;
};
那么,我可以通过只查看成员 m_tpStart
来实现 Timer::WasStarted
吗?我不想为此添加布尔值成员。
So, can I implement Timer::WasStarted by looking only at member m_tpStart?
好吧,如果你定义这样的不变量,当且仅当计时器被重置(未启动)时,m_tpStart
为零(纪元),那么它是微不足道的。只需检查start是否为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 = {};
}