std::chrono::year::year
来自 cppreference.cn
year() = default; |
(1) | (since C++20) |
constexpr explicit year( int y ) noexcept; |
(2) | (since C++20) |
构造一个 year
对象。
1) 默认构造函数将年份值保持为未初始化状态。
2) 如果 y 在范围
[
-32767,
32767]
内,则构造一个 year
对象,该对象持有年份值 y。否则,持有的值是未指定的。[编辑] 示例
运行此代码
#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].