std::chrono::operator+,std::chrono::operator-(std::chrono::year_month) (3) - Linux Manuals

std::chrono::operator+,std::chrono::operator-(std::chrono::year_month): std::chrono::operator+,std::chrono::operator-(std::chrono::year_month)

NAME

std::chrono::operator+,std::chrono::operator-(std::chrono::year_month) - std::chrono::operator+,std::chrono::operator-(std::chrono::year_month)

Synopsis


constexpr std::chrono::year_month operator+(const std::chrono::year_month& ym, (1) (since C++20)
const std::chrono::years& dy) noexcept;
constexpr std::chrono::year_month operator+(const std::chrono::years& dy, (2) (since C++20)
const std::chrono::year_month& ym) noexcept;
constexpr std::chrono::year_month operator+(const std::chrono::year_month& ym, (3) (since C++20)
const std::chrono::months& dm) noexcept;
constexpr std::chrono::year_month operator+(const std::chrono::months& dm, (4) (since C++20)
const std::chrono::year_month& ym) noexcept;
constexpr std::chrono::year_month operator-(const std::chrono::year_month& ym, (5) (since C++20)
const std::chrono::years& dy) noexcept;
constexpr std::chrono::year_month operator-(const std::chrono::year_month& ym, (6) (since C++20)
const std::chrono::months& dm) noexcept;
constexpr std::chrono::months operator-(const std::chrono::year_month& ym1, (7) (since C++20)
const std::chrono::year_month& ym2) noexcept;


1-2) Adds dy.count() years to ym.
3-4) Adds dm.count() months to ym.
5) Subtracts dy.count() years from ym.
6) Subtracts dm.count() months from ym.
7) Returns the difference in months between the two time points represented by ym1 and ym2.

Return value


1-2) std::chrono::year_month(ym.year() + dy, ym.month())
3-4) A year_month value z such that z - ym == dm and z.ok() == true.
5) ym + -dy
5) ym + -dm
7) ym1.year() - ym2.year() + std::chrono::months(int(unsigned(ym1.month())) - int(unsigned(ym2.month())))

Notes


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

Example


 This section is incomplete
 Reason: no example

See also


           modifies the year_month by some number of months or years
operator+= (public member function)
operator-=