Namespaces
Variants

std::chrono::year:: year

From cppreference.net
year ( ) = default ;
(1) (自 C++20 起)
constexpr explicit year ( int y ) noexcept ;
(2) (自 C++20 起)

构造一个 year 对象。

1) 默认构造函数不会初始化年份值。
2) y 在区间 [ - 32767 , 32767 ] 内,则构造持有年份值 y year 对象。否则所持有的值未指定。

示例

#include <chrono>
#include <iostream>
int main()
{
    using namespace std::chrono;
    constexpr int leap_years = []
    {
        int count{};
        for (int i{year::min()}; i <= int{year::max()}; ++i)
            if (year{i}.is_leap()) // 使用构造函数 (2)
                ++count;
        return count;
    } ();
    static_assert(15891 == leap_years);
    std::cout << "在范围 [" << int(year::min()) << ", " << int(year::max()) << "] 中有 " << leap_years << " 个闰年。\n";
}

输出:

在范围 [-32767, 32767] 中有 15891 个闰年。