std::chrono::year_month_weekday_last operator+ / operator-

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

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

출처: cppreference

본문

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

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

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

예를 들어 2023년 9월 마지막 금요일(2023/Sep/Fri[last])에 1년을 더하면 2024년 9월 마지막 금요일(2024/Sep/Fri[last])이 돼요.

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

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

더 알아보기 (Learn more)

cppreference