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

来自cppreference.com

 
 
 
日期和时间工具
(C++11)
(C++11)
时钟
(C++20)
                                                  
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
日历
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
时区
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
C 风格日期和时间
 
 
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;

注意

能从 year_month_weekday 直接加或减可转换到 std::chrono::months 但不可转换到 std::chrono::years 的时长。可转换到 std::chrono::years 的时长不能如此,因为这些时长亦可转换到 std::chrono::months ,导致歧义:

using namespace std::chrono;
 
using decades = duration<int, std::ratio_multiply<std::ratio<10>, years::period>>;
using kilomonths = duration<int, std::ratio_multiply<std::kilo, months::period>>;
 
auto ymwd = 2001y/April/Sunday[1];
ymwd += decades{1}; // 错误:歧义
ymwd += kilomonths{1}; // OK

参阅

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