std::chrono...::operator--(3)
π NAME π
std::chrono::month::operator++, std::chrono::month::operator-- β bestie, these are the cute little in-place-arithmetic operators for std::chrono::month, straight out of the C++ Standard Libary man page π
β¨ SYNOPSIS β¨
constexpr std::chrono::month& operator++() noexcept; (1) (since C++20)
constexpr std::chrono::month operator++( int ) noexcept; (2) (since C++20)
constexpr std::chrono::month& operator--() noexcept; (3) (since C++20)
constexpr std::chrono::month operator--( int ) noexcept; (4) (since C++20)
okay so what these do is add or subtract 1 from the month value, and then reduce the result modulo 12 so it lands on an integer in the range [1, 12]. no cap, it just wraps around like a lil calendar loop π¦
- and 2) perform
*this += std::chrono::months{1};. - and 4) perform
*this -= std::chrono::months{1};.
π PARAMETERS π
(none) β literally nothing, not a single argument, she takes no notes π
πΈ RETURN VALUE πΈ
- 1, 3) a reference to this
monthafter modification π - 2, 4) a copy of the
monthmade before the modification, like a lil before-photo πΈ
π¦ NOTES π¦
after a call to one of these functions, ok() is always true. yes bestie, always. she's that reliable πβ¨
π EXAMPLE π
// Run this code
#include <cassert>
#include <chrono>
#include <iostream>
int main()
{
std::chrono::month m{6};
++m;
assert(m == std::chrono::month(7));
--m;
assert(m == std::chrono::month(6));
m = std::chrono::December;
m++; // rounds up to January
assert(m.ok());
std::cout << unsigned(m) << '\n';
m = std::chrono::January;
m--; // rounds down to December
assert(m.ok());
std::cout << unsigned(m) << '\n';
}
β Output
1
12
see how December rounds up to January and January rounds down to December?? that wrap-around is genuinely iconic, main character energy fr fr π₯
π SEE ALSO π
operator+=β adds or subtracts a number of monthsoperator-=β (public member function)operator+β performs arithmetic on monthsoperator-β (function)- and yeah, all of this is (C++20) era π
mwah π this page came from cppreference.com, dated 2024.06.10, and you're reading the std::chrono...::operator--(3) entry β go forth and increment your months, bestie π«Άβ¨
π¦ 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