std::chrono...::operator-=(3)

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

πŸŽ€ 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. (1) is equivalent to *this = *this + dy;.
  2. (2) is equivalent to *this = *this + dm;.
  3. (3) is equivalent to *this = *this - dy;.
  4. (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 a year_month_weekday and 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