Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::chrono::duration<Rep,Period>::duration

      From cppreference.com
      <cpp‎ |chrono‎ |duration
       
       
      Date and time library
       
      std::chrono::duration
       
      constexpr duration()=default;
      (1)(since C++11)
      duration(const duration&)=default;
      (2)(since C++11)
      template<class Rep2>
      constexprexplicit duration(const Rep2& r);
      (3)(since C++11)
      template<class Rep2,class Period2>
      constexpr duration(const duration<Rep2, Period2>& d);
      (4)(since C++11)

      Constructs a newduration from one of several optional data sources.

      1) The default constructor.
      2) The copy constructor.
      3) Constructs a duration withr ticks.
      This overload participates in overload resolution only if all following conditions are satisfied:
      • is_convertible<const Rep2&, Rep>::value istrue.
      • Any of the following conditions is satisfied:[1]
      4) Constructs a duration by convertingd to an appropriate period and tick count, as if bystd::chrono::duration_cast<duration>(d).count().
      This overload participates in overload resolution only if no overflow is induced in the conversion, and any of the following conditions is satisfied:[2]
      1. That is, a duration with an integer tick count cannot be constructed from a floating-point value, but a duration with a floating-point tick count can be constructed from an integer value.
      2. That is, either the duration uses floating-point ticks, orPeriod2 is exactly divisible byPeriod.

      Contents

      [edit]Parameters

      r - a tick count
      d - a duration to copy from

      [edit]Example

      The following code shows several examples (both valid and invalid) of how to construct durations:

      Run this code
      #include <chrono> int main(){std::chrono::hours h(1);// one hourstd::chrono::milliseconds ms{3};// 3 millisecondsstd::chrono::duration<int,std::kilo> ks(3);// 3000 seconds // error: treat_as_floating_point<int>::value == false,// This duration allows whole tick counts only//  std::chrono::duration<int, std::kilo> d3(3.5); // 30Hz clock using fractional ticksstd::chrono::duration<double,std::ratio<1,30>> hz30(3.5); // 3000 microseconds constructed from 3 millisecondsstd::chrono::microseconds us= ms;// error: 1/1000000 is not divisible by 1/1000//  std::chrono::milliseconds ms2 = usstd::chrono::duration<double,std::milli> ms2= us;// 3.0 milliseconds}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 2094C++11for overload(4),std::ratio_divide<Period2, period>::num
      might overflow when evaluating
      std::ratio_divide<Period2, period>::den
      overload(4) does not
      participate in overload
      resolution in this case
      LWG 3050C++11convertibility constraint used non-const xvalueuse const lvalues instead

      [edit]See also

      assigns the contents
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/chrono/duration/duration&oldid=169900"

      [8]ページ先頭

      ©2009-2025 Movatter.jp