std::chrono::duration(下文简称duration)是标准库中用来代表时间段的一个类模板。QQ:114211200
摘抄部分源码:
template<class _Rep, class _Period = ratio<1> > class duration;//声明
template<class _Rep,
class _Period>
class duration
{ // represents a time duration
public:
typedef duration<_Rep, _Period> _Myt;
typedef _Rep rep;
typedef _Period period;
constexpr _Rep count() const
{ // get stored rep
return (_MyRep);
}
private:
_Rep _MyRep; // the stored rep
};
它有两个参数,第一个参数_Rep代表用什么样的类型来保存这个时间长度,必须是内置数值型或者是功能上类似内置数值型的类型(这个没有试过,应该是提供内置类型的所有接口就可以)。第二个参数_Period必须是std::ratio模板类的一个实例,代表一个时间单位。_Period缺省值为std::ratio<1>,此时它的单位为秒。它只有一个成员变量,用于保存_Period的个数。
duration<int, std::milli>代表以int保存的,单位为1/1000秒(毫秒)的时间长度。duration<int, std::milli> d(5),则d代表5毫秒。
duration<short, std::ratio<1,50>>代表以short保存的,单位为1/50秒的时间长度。duration<short, std::ratio<1,50>> d(50),则d代表1秒。
下面介绍一些重要的成员函数:
template<class _Rep2,
class = typename enable_if<is_convertible<_Rep2, _Rep>::value
&& (treat_as_floating_point<_Rep>::value
|| !treat_as_floating_point<_Rep2>::value),
void>::type>
constexpr explicit duration(const _Rep2& _Val)
: _MyRep(static_cast<_Rep>(_Val))
{ // construct from representation
}
使用另一个数值类型的值来构造。它是使用static_cast()来进行转换的。要求1:_Rep2必须能够隐式转换为_Rep:。要求2:_Rep为浮点型或者_Rep2不是浮点型。否则编译失败。
template<class _Rep2,
class _Period2,
class = typename enable_if<treat_as_floating_point<_Rep>::value
|| (_Ratio_divide_sfinae<_Period2, _Period>::den == 1
&& !treat_as_floating_point<_Rep2>::value),
void>::type>
constexpr duration(const duration<_Rep2, _Period2>& _Dur)
: _MyRep(chrono::duration_cast<_Myt>(_Dur).count())
{ // construct from a duration
}
使用另一个chrono对象来构造,内部通过duration_cast()来转换。以下2种情况可以通过编译:
1._Rep是一种浮点型。
2._Rep和_Rep都是一种整型,此时要求ratio_divide<_Period2, _Period>::type::den必须为1。意思就是_Period2是_Period的整数倍。
这样规定的目的是为了避免精度损失,以及避免从小单位向大单位转换。如果不使用这个构造函数,而直接使用duration_cast()函数,则直接绕过了这些检查,可能会导致精度丢失或数据截断。
constexpr _Rep count() const
{ // get stored rep
return (_MyRep);
}
得到当前对象保存的_Period的个数。
template<class _Rep1,
class _Period1,
class _Rep2,
class _Period2> inline
constexpr bool operator==(
const duration<_Rep1, _Period1>& _Left,
const duration<_Rep2, _Period2>& _Right)
{ // test if duration == duration
typedef typename common_type<
duration<_Rep1, _Period1>,
duration<_Rep2, _Period2> >::type _CT;
return (_CT(_Left).count() == _CT(_Right).count());
}
比较两个duration对象是否想等,由源码得知,比较前先将两个对象转换为通用类型,然后再比较。
例如:duration<int,std::ratio<2,3> >和duration<int,std::ratio<3,7> >都转换为duration<int,std::ratio<1,21> >。
还有min()、max()、zero()函数,分别代表最小、最大、0值,返回的都是自身类型。
剩下的就是一些算术运算和关系运算函数,不再多讲。
另外,stl还定义了一些常用的duration类型,在chrono头文件下:
typedef duration<long long, nano> nanoseconds;//纳秒
typedef duration<long long, micro> microseconds;//微秒
typedef duration<long long, milli> milliseconds;//毫秒
typedef duration<long long> seconds;
typedef duration<int, ratio<60> > minutes;
typedef duration<int, ratio<3600> > hours;
最后给出一个简单的测试程序:
void f()
{
chrono::milliseconds m1(1000 * 60);//毫秒
chrono::duration<int, ratio<1>> m2(60);
chrono::duration<int, ratio<60>> m3(1);
chrono::minutes m4(1);
assert(m1 == m2 && m1 == m3 && m1 == m4);
m1++;
++m1;
m2 = m3 + chrono::duration<int, ratio<60>>(100);//必须显示转换
}