year_month_operator_arith_2

year_month_operator_arith_2 (year_month 산술 연산)

std::chrono::year_month에 대한 operator+, operator- 연산자예요. 년월에 개월 수를 더하거나 빼요.

출처: cppreference

본문

std::chrono::year_month의 산술 연산자는 <chrono> 헤더에 정의되어 있어요. C++20부터 사용 가능해요.

constexpr std::chrono::year_month
    operator+( const std::chrono::year_month& ym,
               const std::chrono::months& dm ) noexcept;

constexpr std::chrono::year_month
    operator-( const std::chrono::year_month& ym,
               const std::chrono::months& dm ) noexcept;

개월 수를 더하거나 빼서 결과 year_month를 반환해요. x + dmxmonth()dm을 더해 넘치면 year()도 조정하고, x - dm은 더한 뒤 -dm을 빼는 방식으로 계산해요.

예제 (Example)

#include <chrono>
#include <iostream>

int main()
{
    using namespace std::chrono;
    year_month ym = 2020y / January + months{14};
    std::cout << ym << '\n'; // 2021-03
}

더 알아보기 (Learn more)

cppreference