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

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

πŸŽ€ 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 πŸ¦‹

  1. and 2) perform *this += std::chrono::months{1};.
  2. 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 month after modification πŸ‘‘
  • 2, 4) a copy of the month made 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 months
  • operator-= β€” (public member function)
  • operator+ β€” performs arithmetic on months
  • operator- β€” (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