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

来自cppreference.com

 
 
 
 
constexpr std::chrono::year_month_weekday_last&
    operator+=( const std::chrono::years& dy ) const noexcept;
(1)(C++20 起)
constexpr std::chrono::year_month_weekday_last&
    operator+=( const std::chrono::months& dm ) const noexcept;
(2)(C++20 起)
constexpr std::chrono::year_month_weekday_last&
    operator-=( const std::chrono::years& dy ) const noexcept;
(3)(C++20 起)
constexpr std::chrono::year_month_weekday_last&
    operator-=( const std::chrono::months& dm ) const noexcept;
(4)(C++20 起)

以时长 dydm 为程度修改 *this 表示的时间点。

1) 等价于 *this = *this + dy;
2) 等价于 *this = *this + dm;
3) 等价于 *this = *this - dy;
4) 等价于 *this = *this - dm;

对于能转换到 std::chrono::yearsstd::chrono::months 两者的时长,若调用有歧义,则偏好 years 的重载 (1,3)

示例

#include <chrono>
#include <iostream>
using namespace std::chrono;
 
int main()
{
    auto ymwdl{August/Friday[last]/2022};
    std::cout << year_month_day{ymwdl} << '\n';
    ymwdl += months(2);
    std::cout << year_month_day{ymwdl} << '\n';
    ymwdl -= years(1); 
    std::cout << year_month_day{ymwdl} << '\n';
}

输出:

2022-08-26
2022-10-28
2021-10-29

参阅

year_month_weekday_last 与一定数量的 yearsmonths 相加或相减
(函数)