std::chrono...:operator+=,(3)
π NAME π
okay so bestie, welcome to the C++ Standard Libary page for std::chrono...:operator+=,(3) β yes, that's the manual name, and yes, we're about to make friends with std::chrono::year_month_weekday_last's compound assignment operators. π
std::chrono::year_month_weekday_last::operator+=, -std::chrono::year_month_weekday_last::operator+=,
not me pointing out that the NAME section says it twice β iconic repetition, honestly π
β¨ Synopsis β¨
the whole squad is constexpr and noexcept, and every single one is since C++20:
constexpr std::chrono::year_month_weekday_last& (1)
(since C++20)
operator+=( const std::chrono::years& dy ) const noexcept;
constexpr std::chrono::year_month_weekday_last& (2)
(since C++20)
operator+=( const std::chrono::months& dm ) const noexcept;
constexpr std::chrono::year_month_weekday_last& (3)
(since C++20)
operator-=( const std::chrono::years& dy ) const noexcept;
constexpr std::chrono::year_month_weekday_last& (4)
(since C++20)
operator-=( const std::chrono::months& dm ) const noexcept;
ngl, that's the entire lineup: overloads (1) and (2) are operator+= with std::chrono::years and std::chrono::months, and overloads (3) and (4) are operator-= with std::chrono::years and std::chrono::months. slay. β¨
Modifies the time point *this represents by the duration dy or dm. main character energy: *this is the one getting changed. π«
- Equivalent to
*this = *this + dy;. - Equivalent to
*this = *this + dm;. - Equivalent to
*this = *this - dy;. - Equivalent to
*this = *this - dm;.
tiny but important vibe check: for durations that are convertible to both std::chrono::years and std::chrono::months, the years overloads (1,3) are preferred if the call would otherwise be ambiguous. no cap, that's literally the rule. π
π Example π
// Run this code
#include <chrono>
#include <iostream>
using namespace std::chrono;
int main()
{
auto ymwdl{August/Friday[last]/2022};
std::cout << year_month_day{ymwdl} << '\n';
ymwdl += months(2);
std::cout << year_month_day{ymwdl} << '\n';
ymwdl -= years(1);
std::cout << year_month_day{ymwdl} << '\n';
}
obsessed with this little demo: it prints the date, adds months(2), prints again, subtracts years(1), and prints one last time. π
π Output: π
2022-08-26
2022-10-28
2021-10-29
ate. periodt. π
π¦ See also π¦
operator+,operator-β adds or subtracts ayear_month_weekday_lastand some number of years or months(C++20)(function).
okay bye bestie π β this lil page came from http://cppreference.com, dated 2024.06.10, and it's the std::chrono...:operator+=,(3) one. π«Ά
π¦ 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