std::chrono...::operator-=(3)
π NAME π
okay bestie, welcome to the std::chrono...::operator-=(3) page from the C++ Standard Libary manual (yes, that's how the header spells it, and I'm not fixing it π
).
The NAME block is doubling up on purpose (or by formatting magic): std::chrono::year_month_weekday::operator+=,std::chrono::year_month_weekday::operator-= β and then the exact same std::chrono::year_month_weekday::operator+=,std::chrono::year_month_weekday::operator-= again, with a lil - sitting between them. It's giving βwe listed the duo twice and we're not sorry.β π
β¨ Synopsis β¨
here are the four overloads, all constexpr, all noexcept, all since C++20. copying them exactly because signatures are sacred:
constexpr std::chrono::year_month_weekday& (1)
(since C++20)
operator+=( const std::chrono::years& dy ) const noexcept;
constexpr std::chrono::year_month_weekday& (2)
(since C++20)
operator+=( const std::chrono::months& dm ) const noexcept;
constexpr std::chrono::year_month_weekday& (3)
(since C++20)
operator-=( const std::chrono::years& dy ) const noexcept;
constexpr std::chrono::year_month_weekday& (4)
(since C++20)
operator-=( const std::chrono::months& dm ) const noexcept;
So what does it actually do? It modifies the time point that *this represents by the duration dy or dm. That's the whole plot, fr fr.
(1)is equivalent to*this = *this + dy;.(2)is equivalent to*this = *this + dm;.(3)is equivalent to*this = *this - dy;.(4)is equivalent to*this = *this - dm;.
and bestie, 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 the tiebreaker. π¦
π¦ Example π¦
run this code, bestie:
// Run this code
#include <cassert>
#include <chrono>
#include <iostream>
int main()
{
auto ymwi{1/std::chrono::Wednesday[2]/2021};
std::cout << ymwi << '\n';
ymwi += std::chrono::years(5);
std::cout << ymwi << '\n';
assert(static_cast<std::chrono::year_month_day>(ymwi) ==
std::chrono::year(2026)/1/14);
ymwi -= std::chrono::months(1);
std::cout << ymwi << '\n';
assert(static_cast<std::chrono::year_month_day>(ymwi) ==
std::chrono::day(10)/12/2025);
}
πΈ Output: πΈ
2021/Jan/Wed[2]
2026/Jan/Wed[2]
2025/Dec/Wed[2]
π See also π
operator+adds or subtracts ayear_month_weekdayand some number of years or months.operator-(function)(C++20).
okay that's the whole page, bestie! source/version: http://cppreference.com; date: 2024.06.10; footer tag: std::chrono...::operator-=(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