std::chrono...::operator-=(3)

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

πŸ’– NAME πŸ’–

okay so bestie, this is the std::chrono...::operator-=(3) page from the C++ Standard Libary manual β€” yes, β€œLibary,” we’re keeping the typo, it’s iconic. The actual functions are std::chrono::weekday::operator+= and std::chrono::weekday::operator-=, and the original NAME line is doing main character energy: std::chrono::weekday::operator+=,std::chrono::weekday::operator-= - std::chrono::weekday::operator+=,std::chrono::weekday::operator-=. no space after that comma, periodt. πŸ’…

✨ Synopsis ✨

the way that both overloads are constexpr, both are noexcept, both return std::chrono::weekday&, and both take const std::chrono::days& d. They’re both (since C++20), fr fr. here’s the synopsis exactly as the page lays it out, labels and all:

          constexpr std::chrono::weekday& operator+=( const
       (1) (since C++20)
          std::chrono::days& d ) noexcept;
          constexpr std::chrono::weekday& operator-=( const
       (2) (since C++20)
          std::chrono::days& d ) noexcept;

the vibe is: these add or subtract d.count() from the weekday value, then reduce the result modulo 7 to an integer in the range [0, 6]. slay.

  1. (1) performs *this = *this + d;.
  2. (2) performs *this = *this - d;.

πŸ’– Return value πŸ’–

A reference to this weekday after modification β€” so it lowkey just hands you back the same weekday object, freshly updated. 🫢

πŸŽ€ Notes πŸŽ€

After a call to one of these functions, ok() is always true if no overflow occurred during the operation. highkey important: no overflow, no drama. ⚠️

πŸ¦‹ Example πŸ¦‹

the original literally says // Run this code, so not me presenting the exact example:

       // Run this code

        #include <chrono>
        #include <iostream>

        int main()
        {
            std::cout << std::boolalpha;

            std::chrono::weekday wd{5}; // Friday is 5
            wd += std::chrono::days(2);
            std::cout << (wd == std::chrono::weekday(0)) << ' '
                      << (wd == std::chrono::Sunday) << ' ';

            wd -= std::chrono::days(3);
            std::cout << (wd == std::chrono::weekday(4)) << ' '
                      << (wd == std::chrono::Thursday) << ' ';

            wd = std::chrono::Wednesday;
            wd += std::chrono::days{8}; // ((3 += 8 == 11) % 7) == 4;
            std::cout << (wd == std::chrono::Thursday) << ' ';

            wd -= (std::chrono::Sunday - std::chrono::Tuesday); // -= -2
            // (4 -= -2) == 6
            std::cout << (wd == std::chrono::Saturday) << '\n';
        }

Output

true true true true true true

🌈 See also 🌈

the whole posse, exactly like the page has it:

operator++
operator++(int) increments or decrements the weekday
operator--      (public member function)
operator--(int)
operator+       performs arithmetic on weekdays
operator-       (function)
(C++20)

iykyk, that’s the reference crew. obsessed. 🧚


kisses bestie πŸ’‹ this cutie page is std::chrono...::operator-=(3), it’s from http://cppreference.com, and it’s dated 2024.06.10. slay, bye ✨

πŸ“¦ where this page comes from