std::chrono::year_month_weekday 연산자

std::chrono::year_month_weekday 연산자 (operator== 등)

std::chrono::year_month_weekday 값을 비교하는 비교 연산자들이에요. 연·월·n번째 요일을 순서대로 비교해요. C++20부터 있어요.

출처: cppreference

본문

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

constexpr bool operator==( const std::chrono::year_month_weekday& x,
                           const std::chrono::year_month_weekday& y ) noexcept;   // (1)

constexpr std::strong_ordering
    operator<=>( const std::chrono::year_month_weekday& x,
                 const std::chrono::year_month_weekday& y ) noexcept;             // (2)

year_month_weekdayxy를 비교해요. 사전식 비교로, 먼저 year()를 비교하고, 그다음 month(), 그다음 weekday_indexed()(요일과 그 인덱스)를 비교해요.

<, <=, >, >=, != 연산자는 각각 operator<=>operator==로부터 합성돼요.

  • 반환 값:
    • (1) x.year() == y.year() && x.month() == y.month() && x.weekday() == y.weekday() && x.index() == y.index()
    • (2) 사전식 비교 결과 (year(), month(), weekday_indexed() 순서).

참고로 두 값이 모두 유효한 날짜를 나타내면, 사전식 비교 결과가 달력 순서와 일치해요.

예제를 보면요.

#include <chrono>

int main()
{
    using namespace std::chrono;
    constexpr auto y1{year_month_weekday{2023y/September/Friday[2]}};
    constexpr auto y2{year_month_weekday{2023y/September/Friday[2]}};
    static_assert(y1 == y2);
}

더 알아보기 (Learn more)

cppreference