std::chrono::year::ok
來自 cppreference.com
constexpr bool ok() const noexcept; |
(C++20 起) | |
檢查儲存在 *this 中的年份值是否在有效範圍內,即 [
-32767,
32767]
。
[編輯] 返回值
如果儲存在 *this 中的年份值在 [
-32767,
32767]
範圍內,則為 true。否則為 false。
[編輯] 可能的實現
參見 libstdc++、libc++ 和 Howard Hinnant 的 date.h 中的實現。
class Year { short year_; // exposition-only public: bool ok() const noexcept { return year_ != std::numeric_limits<short>::min(); } /*...*/ }; |
[編輯] 示例
執行此程式碼
#include <chrono> #include <iomanip> #include <iostream> int main() { std::cout << "input year │ internal value │ ok()\n" << std::boolalpha; for (const int i : {2020, 0x8000, 0x8001, 0xFFFF, 0x18000}) { const std::chrono::year y{i}; std::cout << std::setw(10) << i << " │ " << std::setw(14) << static_cast<int>(y) << " │ " << y.ok() << '\n'; } }
可能的輸出
input year │ internal value │ ok() 2020 │ 2020 │ true 32768 │ -32768 │ false 32769 │ -32767 │ true 65535 │ -1 │ true 98304 │ -32768 │ false