std::理解chrono

//这个是duration的实现
template <class Rep, class Period = ratio<1> >  class duration;

  }
  template <class Rep1, class Period1, class Rep2, class Period2>
  struct common_type<duration<Rep1, Period1>,
                     duration<Rep2, Period2> >;

  namespace chrono {

    // customization traits
    template <class Rep> struct treat_as_floating_point;
    template <class Rep> struct duration_values;

    // duration arithmetic
    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr
    typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
    operator+(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);

    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr
    typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2> >::type
    operator-(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);

    template <class Rep1, class Period, class Rep2>
    constexpr
    duration<typename common_type<Rep1, Rep2>::type, Period>
    operator*(
        const duration<Rep1, Period>& d,
        const Rep2& s);

    template <class Rep1, class Period, class Rep2>
    constexpr
    duration<typename common_type<Rep1, Rep2>::type, Period>
    operator*(
        const Rep1& s,
        const duration<Rep2, Period>& d);

    template <class Rep1, class Period, class Rep2>
    constexpr
    duration<typename common_type<Rep1, Rep2>::type, Period>
    operator/(
        const duration<Rep1, Period>& d,
        const Rep2& s);

    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr
    typename common_type<Rep1, Rep2>::type
    operator/(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);

    #ifdef BOOST_CHRONO_EXTENSIONS
    // Used to get frecuency of events
    template <class Rep1, class Rep2, class Period>
    constexpr
    double operator/(
        const Rep1& s,
        const duration<Rep2, Period>& d);
    #endif

    // duration comparisons
    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr bool operator==(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);

    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr bool operator!=(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);

    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr bool __duration__op_le_1(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);
    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr bool operator<=(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);

    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr bool operator>(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);
    template <class Rep1, class Period1, class Rep2, class Period2>
    constexpr bool operator>=(
        const duration<Rep1, Period1>& lhs,
        const duration<Rep2, Period2>& rhs);

    // duration_cast

    template <class ToDuration, class Rep, class Period>
    constexpr
    ToDuration duration_cast(const duration<Rep, Period>& d);

    // convenience typedefs
    typedef duration<boost::int_least64_t, nano> nanoseconds;    // at least 64 bits needed
    typedef duration<boost::int_least64_t, micro> microseconds;  // at least 55 bits needed
    typedef duration<boost::int_least64_t, milli> milliseconds;  // at least 45 bits needed
    typedef duration<boost::int_least64_t> seconds;              // at least 35 bits needed
    typedef duration<boost::int_least32_t, ratio< 60> > minutes; // at least 29 bits needed
    typedef duration<boost::int_least32_t, ratio<3600> > hours;  // at least 23 bits needed

  }
}
为什么duration会有两个模板参数呢,原因是Rep参数表示的是时间类型的周期,
然后ratio这个是比率,其实简单的理解就是你的时间的单位,所以这里用两个参数是很有道理的,
比如duration>这个代表的是以ratio<60>为单位的时间inteval用int类型表示;
duration> d_time(5.4)这个就错误了  因为不能吧double转成int
这里有一个
 typename common_type<Rep1, Rep2>::type是确定两个类型的通用类型


你可能感兴趣的:(C++)