std::chrono::year::ok(3)
π std::chrono::year::ok π
okay so this page comes from the C++ Standard Libary manual (yes, that spelling, no cap), and the whole vibe is checking whether a std::chrono::year is valid. Itβs giving tiny validity-checker main character energy. π
β¨ NAME β¨
std::chrono::year::ok β literally std::chrono::year::ok. Thatβs the name, bestie, weβre not doing extra.
πΈ Synopsis πΈ
constexpr bool ok() const noexcept;
β¨ since C++20 β¨
This checks if the year value stored in *this is in the valid range, i.e. [-32767, 32767]. Thatβs the whole assignment, and it ate. π₯
π Return value π
It gives you true if the year value stored in *this is in the range [-32767, 32767]. Otherwise? false. Thatβs it. No secret lore. π«Ά
π§ Possible implementation π§
See the implementations in libstdc++, libc++, and Howard Hinnant's date.h.
class Year
{
short year_; // exposition-only
public:
bool ok() const noexcept { return year_ != std::numeric_limits<short>::min(); }
/*...*/
};
π Example π
π // Run this code β go off, bestie:
#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';
}
}
π Possible output π
fr fr it can print like this:
input year β internal value β ok()
2020 β 2020 β true
32768 β -32768 β false
32769 β -32767 β true
65535 β -1 β true
98304 β -32768 β false
okay thatβs the page π source/version + date check: http://cppreference.com, 2024.06.10, and the page is std::chrono::year::ok(3) β mwah π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms