名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | chrono‎ | year_month
 
 
 
 
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;

對於可以轉換為 std::chrono::yearsstd::chrono::months 的持續時間,如果呼叫會產生歧義,則首選 years 過載 (1,3)

[編輯] 示例

#include <cassert>
#include <chrono>
 
int main()
{
    auto ym{std::chrono::day(1)/7/2023};
 
    ym -= std::chrono::years{2};
    assert(ym.month() == std::chrono::July);
    assert(ym.year() == std::chrono::year(2021));
 
    ym += std::chrono::months{7};
    assert(ym.month() == std::chrono::month(2));
    assert(ym.year() == std::chrono::year(2022));
}

[編輯] 參閱

year_month 進行算術運算
(函式) [編輯]