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

📦 version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ NAME ✨

Bestie, this is page std::chrono...::operator-=(3) from the C++ Standard Libary manual (yes, “Libary” is the manual’s spelling, not a typo from me 💀). The NAME line is exactly:

std::chrono::day::operator+=,std::chrono::day::operator-= - std::chrono::day::operator+=,std::chrono::day::operator-=

It’s giving “two operators, one page” energy. 💅

✨ Synopsis ✨

Two overloads, both marked noexcept and both (since C++20). Here are the signatures, character for character:

(1) (since C++20)

constexpr std::chrono::day& operator+=( const std::chrono::days& d ) noexcept;

(2) (since C++20)

constexpr std::chrono::day& operator-=( const std::chrono::days& d ) noexcept;

The manual says it adds or subtracts d.count() days from the day value. Slay.

  • (1) is equivalent to *this = *this + d;.
  • (2) is equivalent to *this = *this - d;.

🫶 Return value 🫶

A reference to this day after modification. Yep, it hands you back a reference to the modified day. 🫶

⚠️ Notes ⚠️

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

That’s the manual’s wording, and “unspecified” is doing a lot of heavy lifting there. No cap. 😭

🎀 Example 🎀

The page literally says // Run this code, so here’s the exact code:

#include <cassert>
#include <chrono>

int main()
{
    std::chrono::day d{15};

    d += std::chrono::days(2);
    assert(d == std::chrono::day(17));

    d -= std::chrono::days{3};
    assert(d == std::chrono::day(14));
}

It starts at std::chrono::day d{15}, does d += std::chrono::days(2), checks d == std::chrono::day(17), then does d -= std::chrono::days{3}, and checks d == std::chrono::day(14). Iconic. ✨

👑 See also 👑

  • operator++, operator++(int), operator--, operator--(int) — increments or decrements the day (public member function)
  • operator+, operator- — adds or subtracts a number of days and a day, or find the difference between two days (function) (C++20)

Source/version/date sign-off, bestie: the footer says http://cppreference.com, 2024.06.10, and page std::chrono...::operator-=(3). Go forth and subtract days with main character energy 💜🦋

📦 where this page comes from