Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::common_type<std::chrono::duration>

      From cppreference.com
      <cpp‎ |chrono‎ |duration
       
       
      Date and time library
       
      std::chrono::duration
       
      Defined in header<chrono>
      template<class Rep1,class Period1,class Rep2,class Period2>

      struct common_type<std::chrono::duration<Rep1, Period1>,

                         std::chrono::duration<Rep2, Period2>>;
      (since C++11)

      Exposes the type namedtype, which is the common type of twostd::chrono::durations, whose period is the greatest common divisor ofPeriod1 andPeriod2.

      Contents

      [edit]Member types

      Member type Definition
      typestd::chrono::duration<typenamestd::common_type<Rep1, Rep2>::type,/* see note */>

      [edit]Note

      The period of the resulting duration can be computed by forming a ratio of the greatest common divisor ofPeriod1::num andPeriod2::num and the least common multiple ofPeriod1::den andPeriod2::den.

      [edit]Example

      Run this code
      #include <chrono>#include <iostream>#include <type_traits> // std::chrono already finds the greatest common divisor,// likely using std::common_type<>. We make the type// deduction externally. template<typename T,typename S>constexprauto durationDiff(const T& t,const S& s)->typenamestd::common_type<T,S>::type{typedeftypenamestd::common_type<T,S>::type Common;return Common(t)- Common(s);} int main(){usingnamespace std::literals; constexprauto ms= 30ms;constexprauto us= 1100us;constexprauto diff= durationDiff(ms, us); std::cout<< ms<<" - "<< us<<" = "<< diff<<'\n';}

      Output:

      30ms - 1100us = 28900us

      [edit]See also

      specializes thestd::common_type trait
      (class template specialization)[edit]
      determines the common type of a group of types
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/chrono/duration/common_type&oldid=175510"

      [8]ページ先頭

      ©2009-2025 Movatter.jp