constexpr std::chrono::year_month operator+(const std::chrono::year_month& ym, const std::chrono::years& dy) noexcept; | (1) | (since C++20) |
constexpr std::chrono::year_month operator+(const std::chrono::years& dy, const std::chrono::year_month& ym) noexcept; | (2) | (since C++20) |
constexpr std::chrono::year_month operator+(const std::chrono::year_month& ym, const std::chrono::months& dm) noexcept; | (3) | (since C++20) |
constexpr std::chrono::year_month operator+(const std::chrono::months& dm, const std::chrono::year_month& ym) noexcept; | (4) | (since C++20) |
constexpr std::chrono::year_month operator-(const std::chrono::year_month& ym, const std::chrono::years& dy) noexcept; | (5) | (since C++20) |
constexpr std::chrono::year_month operator-(const std::chrono::year_month& ym, const std::chrono::months& dm) noexcept; | (6) | (since C++20) |
constexpr std::chrono::months operator-(const std::chrono::year_month& ym1, const std::chrono::year_month& ym2) noexcept; | (7) | (since C++20) |
dy.count()
years to ym
. dm.count()
months to ym
. dy.count()
years from ym
.dm.count()
months from ym
.ym1
and ym2
.std::chrono::year_month(ym.year() + dy, ym.month())
year_month
value z
such that z - ym == dm
and z.ok() == true
.ym + -dy
ym + -dm
ym1.year() - ym2.year() + std::chrono::months(int(unsigned(ym1.month())) - int(unsigned(ym2.month())))
The result of subtracting two year_month
values is a duration of type std::chrono::months
. This duration unit represents the length of the average Gregorian month (30.436875 days), and the resulting duration bears no relationship to the actual number of days in the time period at issue. For example, the result of 2017y/3 - 2017y/2
is std::chrono::months(1)
, even though February 2017 only contains 28 days.
Durations that are convertible to std::chrono::months
, but not std::chrono::years
, can be directly added to or subtracted from a year_month
. Durations convertible to std::chrono::years
cannot because such durations are also convertible to std::chrono::months
, resulting in an ambiguity:
using namespace std::chrono; using decades = duration<int, std::ratio_multiply<std::ratio<10>, years::period>>; using kilomonths = duration<int, std::ratio_multiply<std::kilo, months::period>>; auto ym = 2001y/April; ym = ym + decades{1}; // error, ambiguous ym = ym + kilomonths{1}; // OK
modifies the year_month by some number of months or years (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/chrono/year_month/operator_arith_2