名稱空間
變體
操作

std::chrono::year::year

來自 cppreference.com
< cpp‎ | chrono‎ | year
 
 
 
 
year() = default;
(1) (C++20 起)
constexpr explicit year( int y ) noexcept;
(2) (C++20 起)

構造一個 year 物件。

1) 預設建構函式將年份值保留為未初始化。
2) 如果 y 在範圍 [-3276732767] 內,構造一個持有年份值 yyear 物件。否則,持有的值未指定。

[編輯] 示例

#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()) // uses constructor (2)
                ++count;
        return count;
    } ();
 
    static_assert(15891 == leap_years);
 
    std::cout << "There are " << leap_years << " leap years in the range ["
              << int(year::min()) << ", " << int(year::max()) << "].\n";
}

輸出

There are 15891 leap years in the range [-32767, 32767].