std::chrono::year::ok
来自cppreference.com
| |
(C++20 起) | |
检查存储于 *this 的年份值是否在合法范围,即 [-32767, 32767] 中。
返回值
若存储于 *this 的年份值在范围 [-32767, 32767] 中则为 true 。否则为 false 。
可能的实现
参见 libstdc++、 libc++ 和 Howard Hinnant 的 date.h 中的实现。
class Year
{
short year_; // 仅用于阐释
public:
bool ok() const noexcept { return year_ != std::numeric_limits<short>::min(); }
/*...*/
};
|
示例
运行此代码
#include <chrono>
#include <iomanip>
#include <iostream>
int main()
{
std::cout << "输入年份 │ 内部值 │ ok()\n" << std::boolalpha;
for (const int i : {2020, 0x8000, 0x8001, 0xFFFF, 0x18000})
{
const std::chrono::year y{i};
std::cout << std::setw(7) << i << " │ "
<< std::setw(6) << static_cast<int>(y) << " │ "
<< y.ok() << '\n';
}
}
可能的输出:
输入年份 │ 内部值 │ ok()
2020 │ 2020 │ true
32768 │ -32768 │ false
32769 │ -32767 │ true
65535 │ -1 │ true
98304 │ -32768 │ false