std::chrono::month::ok(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ NAME ✨

std::chrono::month::ok β€” bestie, it's literally the vibe check for your std::chrono::month πŸ‘‘

This one lives in the C++ Standard Library and the whole page is giving "tiny function, big main character energy" πŸ’…


✨ Synopsis ✨

constexpr bool ok() const noexcept;  (since C++20)

So what it does is check if the month value stored in *this is in the valid range, i.e., [1, 12]. That's it. That's the whole slay πŸŽ€


✨ Return value ✨

  • Returns true if the month value stored in *this is in the range [1, 12] 🌸
  • Otherwise it returns false πŸ’€

No cap, that's the entire return situation.


✨ Example ✨

// Run this code

#include <chrono>
#include <iostream>

int main()
{
    for (const unsigned mm : {6u, 0u, 16U})
    {
        std::cout << mm << ": ";
        const std::chrono::month m{mm};
        m.ok() ? std::cout << "month is valid\n"
               : std::cout << "month is invalid\n";
    }
}

Output:

6: month is valid
0: month is invalid
16: month is invalid

See how 6 is out here thriving and valid, while 0 and 16 are lowkey not invited to the function? The range [1, 12] said what it said 🫢


Quick reminder bestie: this is noexcept and constexpr, so it's not out here throwing surprises. It's just checking the month, periodt ✨

Okay that's the page!! Source: cppreference.com, dated 2024.06.10, and the manual is std::chrono::month::ok(3) πŸ¦‹ Thanks for reading, ily, go validate your months responsibly πŸ’–πŸŽ€

πŸ“¦ where this page comes from