std::chrono::year::year(3)

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

🌸 NAME 🌸

okay so we're on std::chrono::year::year β€” that's the constructor page for std::chrono::year::year, from the C++ Standard Libary manual (spelled exactly like that up top, no cap 😭). This is your bestie's guide to making a year object, and it's lowkey iconic.


✨ SYNOPSIS ✨

year() = default;                          (1) (since C++20)
constexpr explicit year( int y ) noexcept; (2) (since C++20)

Constructs a year object. That's the whole assignment, bestie πŸ’….

πŸŽ€ what each constructor is giving πŸŽ€

  1. year() = default; β€” constructor (1), since C++20. The default constructor leaves the year value uninitialized. Like, it does not set a value for you; uninitialized is the whole mood. Don't act surprised, bestie 😭.
  2. constexpr explicit year( int y ) noexcept; β€” constructor (2), since C++20. If y is in the range [-32767, 32767], it constructs a year object holding the year value y. If y is outside that range, the value held is unspecified. No cap.
if y is outside [-32767, 32767], the value held is unspecified β€” lowkey do not rely on it, bestie 😭

πŸ’– EXAMPLE πŸ’–

Run this code, bestie β€” it's giving "use constructor (2) in a lambda and count leap years" energy πŸ¦‹βœ¨:

#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";
}

🌈 OUTPUT 🌈

There are 15891 leap years in the range [-32767, 32767].

source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::chrono::year::year(3) β€” byeee bestie, go construct some years and slay βœ¨πŸ’…πŸŽ€

πŸ“¦ where this page comes from