std::chrono::year::year
来自cppreference.com
| |
(1) | (C++20 起) |
| |
(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 个闰年。