year_month_day_operator_arith_2

year_month_day_operator_arith_2 (year_month_day 산술 연산)

std::chrono::year_month_day에 대한 operator+, operator- 연산자예요. 날짜에 days 또는 months, years를 더하거나 빼요.

출처: cppreference

본문

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

constexpr std::chrono::sys_time<std::chrono::days>
    operator+( const std::chrono::year_month_day& ymd,
               const std::chrono::days& d ) noexcept;

constexpr std::chrono::sys_time<std::chrono::days>
    operator-( const std::chrono::year_month_day& ymd,
               const std::chrono::days& d ) noexcept;

ymd + dsys_days{ymd} + d와 동등하고, ymd - dsys_days{ymd} - d와 동등해요. months/years를 더하는 연산도 지원돼요.

예제 (Example)

#include <chrono>
#include <iostream>

int main()
{
    using namespace std::chrono;
    year_month_day ymd = 2020y / July / 13;
    auto d = ymd + days{20};
    std::cout << year_month_day{d} << '\n'; // 2020-08-02
}

더 알아보기 (Learn more)

cppreference