std::chrono...o::duration)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– std::chrono::abs(std::chrono::duration) πŸ’–

Hey bestie! You're reading the std::chrono::abs(std::chrono::duration) page from the C++ Standard Libary manual. Yes, that's the manual name, and yes, we're keeping it exactly like that. πŸ’…


🌸 NAME 🌸

std::chrono::abs(std::chrono::duration) β€” std::chrono::abs(std::chrono::duration)

(The page literally names it twice, so I'm literally naming it twice. Iconic behavior. 🫢)


✨ Synopsis ✨

Defined in header <chrono>:

template< class Rep, class Period >
constexpr std::chrono::duration<Rep, Period> abs(
(since C++17)
std::chrono::duration<Rep, Period> d );

C++17 girlies, this one's for you. πŸ’œ

Okay so what does it do? It returns the absolute value of the duration d. Specifically: if d >= d.zero(), return d; otherwise return -d. That's the whole vibe check. ✨

Heads up though, because this part matters: the function does not participate in overload resolution unless std::numeric_limits<Rep>::is_signed is true. That's a hard requirement, no cap. πŸ”₯


πŸŽ€ Parameters πŸŽ€

  • d β€” duration

πŸ¦‹ Return value πŸ¦‹

Absolute value of d. Slay. πŸ’–


πŸ‘‘ Possible implementation πŸ‘‘

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

🌈 Example 🌈

// Run this code

#include <chrono>
#include <iostream>

int main()
{
    using namespace 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) = 3
abs(-3min) = 3

πŸ‘€ See also πŸ‘€

  • operator+ β€” implements unary + and unary -
  • operator- β€” (public member function)
  • abs(int)
  • labs β€” computes absolute value of an integral value (\(\small{|x|}\)|x|) (function)
  • llabs β€” (function) (C++11)

That's the whole page, bestie! Big kiss from the C++ Standard Libary crew πŸ’‹ β€” source: http://cppreference.com, version: 2024.06.10, and date: 2024.06.10. The footer title ting was std::chrono...o::duration)(3), mwah! πŸ’–πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from