Namespaces
Variants

std::chrono::year:: ok

From cppreference.net
constexpr bool ok ( ) const noexcept ;
(C++20 이후)

* this 에 저장된 연도 값이 유효한 범위, 즉 [ - 32767 , 32767 ] 내에 있는지 확인합니다.

반환값

true 를 반환합니다, 만약 * this 에 저장된 연도 값이 [ - 32767 , 32767 ] 범위 내에 있을 경우. 그렇지 않으면 false 를 반환합니다.

가능한 구현

구현 내용은 libstdc++ , libc++ , 그리고 Howard Hinnant의 date.h 에서 확인하십시오.

class Year
{
    short year_;   // 설명 전용
public:
    bool ok() const noexcept { return year_ != std::numeric_limits<short>::min(); }
    /*...*/
};

예제

#include <chrono>
#include <iomanip>
#include <iostream>
int main()
{
    std::cout << "input year │ internal value │ ok()\n" << std::boolalpha;
    for (const int i : {2020, 0x8000, 0x8001, 0xFFFF, 0x18000})
    {
        const std::chrono::year y{i};
        std::cout << std::setw(10) << i << " │ "
                  << std::setw(14) << static_cast<int>(y) << " │ "
                  << y.ok() << '\n';
    }
}

가능한 출력:

input year │ internal value │ ok()
      2020 │           2020 │ true
     32768 │         -32768 │ false
     32769 │         -32767 │ true
     65535 │             -1 │ true
     98304 │         -32768 │ false