Namespaces
Variants

std::chrono::year_month:: operator+=, std::chrono::year_month:: operator-=

From cppreference.net

constexpr std:: chrono :: year_month &
operator + = ( const std:: chrono :: years & dy ) const noexcept ;
(1) (seit C++20)
constexpr std:: chrono :: year_month &
operator + = ( const std:: chrono :: months & dm ) const noexcept ;
(2) (seit C++20)
constexpr std:: chrono :: year_month &
operator - = ( const std:: chrono :: years & dy ) const noexcept ;
(3) (seit C++20)
constexpr std:: chrono :: year_month &
operator - = ( const std:: chrono :: months & dm ) const noexcept ;
(4) (seit C++20)

Modifiziert den Zeitpunkt, den * this repräsentiert, um die Dauer dy oder dm .

1) Entspricht * this = * this + dy ; .
2) Entspricht * this = * this + dm ; .
3) Entspricht * this = * this - dy ; .
4) Entspricht * this = * this - dm ; .

Für Zeitdauern, die sowohl in std::chrono::years als auch in std::chrono::months konvertierbar sind, werden die years Überladungen (1,3) bevorzugt, falls der Aufruf andernfalls mehrdeutig wäre.

Beispiel

#include <cassert>
#include <chrono>
int main()
{
    auto ym{std::chrono::day(1)/7/2023};
    ym -= std::chrono::years{2};
    assert(ym.month() == std::chrono::July);
    assert(ym.year() == std::chrono::year(2021));
    ym += std::chrono::months{7};
    assert(ym.month() == std::chrono::month(2));
    assert(ym.year() == std::chrono::year(2022));
}

Siehe auch

führt arithmetische Operationen auf year_month durch
(Funktion)