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

来自cppreference.com
< cpp‎ | chrono‎ | year month

 
 
 
日期和时间工具
(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& operator+=(const std::chrono::years& dy) const noexcept;
(1) (C++20 起)
constexpr std::chrono::year_month& operator+=(const std::chrono::months& dm) const noexcept;
(2) (C++20 起)
constexpr std::chrono::year_month& operator-=(const std::chrono::years& dy) const noexcept;
(3) (C++20 起)
constexpr std::chrono::year_month& 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 直接加或减可转换到 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 ym = 2001y/April;
ym += decades{1}; // 错误:歧义
ym += kilomonths{1}; // OK

参阅

进行 year_month 上的算术
(函数)