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

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

πŸ’– NAME πŸ’–

Welcome to std::chrono...::operator-=(3), bestie! This page covers std::chrono::year::operator+= and std::chrono::year::operator-= β€” the NAME line serves them as std::chrono::year::operator+=,std::chrono::year::operator-= and then repeats std::chrono::year::operator+=,std::chrono::year::operator-= after the dash. Periodt. It’s from the C++ Standard Libary manual (yes, β€œLibary” β€” the typo is canon, don’t come for me πŸ’€).


🌸 Synopsis 🌸

Here’s the signature tea, verbatim, because we do not mess with code:

          constexpr std::chrono::year& operator+=( const std::chrono::years&
       (1) (since C++20)
          y ) noexcept;
          constexpr std::chrono::year& operator-=( const std::chrono::years&
       (2) (since C++20)
          y ) noexcept;

In human: it adds or subtracts y.count() years from the year value. πŸ¦‹

  1. (1) β€” Equivalent to *this = *this + y;.
  2. (2) β€” Equivalent to *this = *this - y;.

🫢 Return value 🫢

You get back a reference to this year after modification. Like it hands you the same lil’ object, all updated and ready. 🫢


🧚 Notes 🧚

If the result would be outside the range [-32767, 32767], the actual stored value is unspecified.

No cap, that’s literally what the page says. ⚠️


πŸ’… Example πŸ’…

The page says // Run this code, and then serves this exact code:

        #include <chrono>
        #include <iostream>

        int main()
        {
            using namespace std::literals::chrono_literals;
            std::cout << std::boolalpha;

            std::chrono::year y{2020};

            y += std::chrono::years(12);
            std::cout << (y == 2032y) << ' ';

            y -= std::chrono::years(33);
            std::cout << (y == 1999y) << '\n';
        }

🌈 Output: 🌈

It outputs exactly:

true true

πŸ‘‘ See also πŸ‘‘

  • operator++
  • operator++(int) β€” increments or decrements the year
  • operator-- β€” (public member function)
  • operator--(int)
  • operator+ β€” performs arithmetic on years
  • operator- β€” (function)
  • (C++20)

Cute sign-off: this page came from http://cppreference.com, dated 2024.06.10, and it’s std::chrono...::operator-=(3). Bye bestie, go slay some years! πŸ’–βœ¨

πŸ“¦ where this page comes from