std::chrono...::operator-=(3)
πΈ std::chrono...::operator-=(3) πΈ
okay bestie, this one is from the C++ Standard Libary manual (yes, "Libary" β I'm not fixing the header spelling, that's literally what it says π). It's a C++ chrono page, and yes, the title says operator-= but the page is also serving operator+= energy. We're covering both, no cap.
β¨ NAME β¨
The page is literally named this mouthful, and the original repeats it twice:
std::chrono::year_month_day_last::operator+=,std::chrono::year_month_day_last::operator-=
-
std::chrono::year_month_day_last::operator+=,std::chrono::year_month_day_last::operator-=
So it's the std::chrono::year_month_day_last::operator+= and std::chrono::year_month_day_last::operator-= page. Iconic, verbose, very C++.
π Synopsis π
Four overloads, all constexpr, all noexcept, all since C++20. Here's the exact lineup:
constexpr std::chrono::year_month_day_last& (1)
(since C++20)
operator+=( const std::chrono::years& dy ) const noexcept;
constexpr std::chrono::year_month_day_last& (2)
(since C++20)
operator+=( const std::chrono::months& dm ) const noexcept;
constexpr std::chrono::year_month_day_last& (3)
(since C++20)
operator-=( const std::chrono::years& dy ) const noexcept;
constexpr std::chrono::year_month_day_last& (4)
(since C++20)
operator-=( const std::chrono::months& dm ) const noexcept;
So what's the vibe? It modifies the time point *this represents by the duration dy or dm. That's the main character move.
- Equivalent to
*this = *this + dy;. - Equivalent to
*this = *this + dm;. - Equivalent to
*this = *this - dy;. - Equivalent to
*this = *this - dm;.
tiny but important detail: for durations that are convertible to bothstd::chrono::yearsandstd::chrono::months, the years overloads(1,3)are preferred if the call would otherwise be ambiguous. That's the rule, bestie β don't come for me, I'm just reporting it. π«Ά
π Example π
go run this, it's giving chrono confidence:
// Run this code
#include <cassert>
#include <chrono>
int main()
{
auto ymdl{11/std::chrono::last/2020};
ymdl += std::chrono::years(15);
assert(ymdl.day() == std::chrono::day(30));
assert(ymdl.month() == std::chrono::November);
assert(ymdl.year() == std::chrono::year(2035));
ymdl -= std::chrono::months(6);
assert(ymdl.day() == std::chrono::day(31));
assert(ymdl.month() == std::chrono::May);
assert(ymdl.year() == std::chrono::year(2035));
}
π¦ See also π¦
operator+β adds or subtracts ayear_month_day_lastand some number of years or monthsoperator-(function) β (C++20)
kisses bestie π this was the std::chrono...::operator-=(3) page from http://cppreference.com, dated 2024.06.10. Now go flex that calendar math. πβ¨
π¦ 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