命名空间
变体
操作

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

出自 cppreference.cn
 
 
 
 
constexpr std::chrono::year_month_weekday&
    operator+=( const std::chrono::years& dy ) const noexcept;
(1) (C++20 起)
constexpr std::chrono::year_month_weekday&
    operator+=( const std::chrono::months& dm ) const noexcept;
(2) (C++20 起)
constexpr std::chrono::year_month_weekday&
    operator-=( const std::chrono::years& dy ) const noexcept;
(3) (C++20 起)
constexpr std::chrono::year_month_weekday&
    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)

[edit] 示例

#include <cassert>
#include <chrono>
#include <iostream>
 
int main()
{
    auto ymwi{1/std::chrono::Wednesday[2]/2021};
    std::cout << ymwi << '\n';
 
    ymwi += std::chrono::years(5);
    std::cout << ymwi << '\n';
    assert(static_cast<std::chrono::year_month_day>(ymwi) ==
                       std::chrono::year(2026)/1/14);
 
    ymwi -= std::chrono::months(1);
    std::cout << ymwi << '\n';
    assert(static_cast<std::chrono::year_month_day>(ymwi) == 
                       std::chrono::day(10)/12/2025);
}

输出

2021/Jan/Wed[2]
2026/Jan/Wed[2]
2025/Dec/Wed[2]

[edit] 参见

添加或减去 year_month_weekday 以及若干年或月
(函数) [编辑]