std::chrono...::operator-=(3)
π 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)performs*this = *this + d;.(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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms