std::chrono...th_day_last)(3)
π std::chrono::operator+, std::chrono::operator-(std::chrono::year_month_day_last) π
okay so bestie, this page is from the C++ Standard Libary manual (yes, the header literally spells it βLibaryβ β not my typo, itβs iconic), and the section is std::chrono...th_day_last)(3). Weβre talking about the std::chrono::operator+ and std::chrono::operator- overloads for std::chrono::year_month_day_last. Itβs giving calendar math main character energy. ποΈβ¨
π NAME π
std::chrono::operator+,std::chrono::operator-(std::chrono::year_month_day_last) β and then after the dash it says the exact same thing again: std::chrono::operator+,std::chrono::operator-(std::chrono::year_month_day_last). Itβs literally doubled. No cap. π
β¨ Synopsis β¨
Defined in header <chrono>.
All of these are constexpr, all of them are noexcept, and all of them are (since C++20). The original sprinkles (since C++20) between the parameters like confetti, but the overloads themselves are all C++20 babies. πΌ
constexpr std::chrono::year_month_day_last operator+( const std::chrono::year_month_day_last& ymdl, const std::chrono::months& dm ) noexcept;
constexpr std::chrono::year_month_day_last operator+( const std::chrono::months& dm, const std::chrono::year_month_day_last& ymdl ) noexcept;
constexpr std::chrono::year_month_day_last operator+( const std::chrono::year_month_day_last& ymdl, const std::chrono::years& dy ) noexcept;
constexpr std::chrono::year_month_day_last operator+( const std::chrono::years& dy, const std::chrono::year_month_day_last& ymdl ) noexcept;
constexpr std::chrono::year_month_day_last operator-( const std::chrono::year_month_day_last& ymdl, const std::chrono::months& dm ) noexcept;
constexpr std::chrono::year_month_day_last operator-( const std::chrono::year_month_day_last& ymdl, const std::chrono::years& dy ) noexcept;
so hereβs the tea on what each numbered overload actually does π
- , 2. Adds
dm.count()months to the date represented byymdl. The result has the sameyear()andmonth()asstd::chrono::year_month(ymdl.year(), ymdl.month()) + dm. - , 4. Adds
dy.count()years to the date represented byymdl. The result is equivalent tostd::chrono::year_month_day_last(ymdl.year() + dy, ymdl.month_day_last()). - Subtracts
dm.count()months from the date represented byymdl. Equivalent toymdl + -dm. - Subtracts
dy.count()years from the date represented byymdl. Equivalent toymdl + -dy.
Important vibe check: for durations that are convertible to bothstd::chrono::yearsandstd::chrono::months, the years overloads (3,4,6) are preferred if the call would otherwise be ambiguous.
π Example π
// Run this code
#include <cassert>
#include <chrono>
#include <iostream>
int main()
{
auto ymdl{11/std::chrono::last/2020};
std::cout << ymdl << '\n';
ymdl = std::chrono::years(10) + ymdl;
std::cout << ymdl << '\n';
assert(ymdl == std::chrono::day(30)/
std::chrono::November/
std::chrono::year(2030));
ymdl = ymdl - std::chrono::months(6);
std::cout << ymdl << '\n';
assert(ymdl == std::chrono::day(31)/
std::chrono::May/
std::chrono::year(2030));
}
πΈ Output πΈ
2020/Nov/last
2030/Nov/last
2030/May/last
thatβs the whole page bestie π«Ά this came from http://cppreference.com, dated 2024.06.10, and the footer literally says std::chrono...th_day_last)(3) β go slay some calendars π
β¨
π¦ 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