std::chrono::year::year
来自 cppreference.com
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()) // 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].