std::chrono...o::duration)(3)
π 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms