Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::chrono::abs(std::chrono::duration)

      From cppreference.com
      <cpp‎ |chrono‎ |duration
       
       
      Date and time library
       
      std::chrono::duration
       
      Defined in header<chrono>
      template<class Rep,class Period>
      constexprstd::chrono::duration<Rep, Period> abs(std::chrono::duration<Rep, Period> d);
      (since C++17)

      Returns the absolute value of the durationd. Specifically, ifd>= d.zero(), returnd, otherwise return-d.

      The function does not participate in the overload resolution unlessstd::numeric_limits<Rep>::is_signed istrue.

      Contents

      [edit]Parameters

      d - duration

      [edit]Return value

      Absolute value ofd.

      [edit]Possible implementation

      template<class Rep,class Period,class=std::enable_if_t<std::numeric_limits<Rep>::is_signed>>constexprstd::chrono::duration<Rep, Period> abs(std::chrono::duration<Rep, Period> d){return d>= d.zero()?+d:-d;}

      [edit]Example

      Run this code
      #include <chrono>#include <iostream> int main(){usingnamespace std::chrono;     static_assert(abs(-42s)== std::chrono::abs(42s)); std::cout<<"abs(+3min) = "<< abs(3min).count()<<'\n'<<"abs(-3min) = "<< abs(-3min).count()<<'\n';}

      Output:

      abs(+3min) = 3abs(-3min) = 3

      [edit]See also

      implements unary + and unary -
      (public member function)[edit]
      computes absolute value of an integral value (\(\small{|x|}\)|x|)
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/chrono/duration/abs&oldid=158855"

      [8]ページ先頭

      ©2009-2025 Movatter.jp