std::chrono::year_month_weekday operator+ / operator-

std::chrono::year_month_weekday operator+ / operator- (산술 연산)

std::chrono::year_month_weekday에 몇 년 또는 몇 달을 더하거나 빼는 산술 연산자들이에요. C++20부터 있어요.

출처: cppreference

본문

<chrono> 헤더에 정의돼 있고, year_month_weekday용 산술 연산자예요.

constexpr std::chrono::year_month_weekday
    operator+( const std::chrono::year_month_weekday& ymwd,
               const std::chrono::months& dm ) noexcept;   // (1)
// 등 (months, years에 대해 +와 - 의 다양한 순서 조합)

ymwd에서 몇 달(dm) 또는 몇 년(dy)을 더하거나 빼요. 결과는 ymwd의 해당 멤버에 operator+=/operator-=를 적용한 것과 같아요.

예를 들어 2023년 9월 두 번째 금요일(2023/Sep/Fri[2])에 1개월을 더하면 2023년 10월 두 번째 금요일(2023/Oct/Fri[2])이 돼요.

  • 반환 값: 월/년이 반영된 year_month_weekday.
#include <chrono>
#include <iostream>

int main()
{
    using namespace std::chrono;
    auto ymwd = 2023y / September / Friday[2];
    auto next = ymwd + months{1};   // 2023/Oct/Fri[2]
    auto prev = ymwd - years{1};    // 2022/Sep/Fri[2]
    std::cout << next << '\n' << prev << '\n';
}

더 알아보기 (Learn more)

cppreference