名稱空間
變體
操作

std::chrono::operator+, std::chrono::operator- (std::chrono::year)

來自 cppreference.com
< cpp‎ | chrono‎ | year
 
 
 
 
constexpr std::chrono::year operator+( const std::chrono::year& y,
                                       const std::chrono::years& ys ) noexcept;
(1) (C++20 起)
constexpr std::chrono::year operator+( const std::chrono::years& ys,
                                       const std::chrono::year& y ) noexcept;
(2) (C++20 起)
constexpr std::chrono::year operator-( const std::chrono::year& y,
                                       const std::chrono::years& ys ) noexcept;
(3) (C++20 起)
constexpr std::chrono::years operator-( const std::chrono::year& y1,
                                        const std::chrono::year& y2 ) noexcept;
(4) (C++20 起)
1,2)ys.count() 年新增到 y
3)y 中減去 ys.count() 年。
4) 返回 y1y2 之間的年份差。

目錄

[edit] 返回值

1,2) std::chrono::year(int(y) + ys.count())
3) std::chrono::year(int(y) - ys.count())
4) std::chrono::years(int(y1) - int(y2))

[edit] 注意

如果 (1-3) 的結果年份值超出範圍 [-3276732767],則實際儲存的值未指定。

兩個 year 值相減的結果是型別為 std::chrono::years 的 duration。這個 duration 單位表示平均公曆年的長度,並且結果 duration 與運算元所表示的特定年份的天數無關。例如,2018y - 2017y 的結果是 std::chrono::years(1),它表示 365.2425 天,而不是 365 天。

[edit] 示例

#include <cassert>
#include <chrono>
 
int main()
{
    std::chrono::year y{2020};
 
    y = std::chrono::years(12) + y; // overload (2): duration + time point
    assert(y == std::chrono::year(2032));
 
    y = y - std::chrono::years(33); // overload (3): time point - duration
    assert(y == std::chrono::year(1999));
 
    // y = std::chrono::years(33) - y; // not supported: duration - time point
 
    using namespace std::chrono;
    constexpr std::chrono::years ys = 2025y - 2020y; // overload (4)
    static_assert(ys == std::chrono::years(5));
}

[edit] 參閱

增加或減少月份
(std::chrono::month 的公開成員函式) [編輯]
增加或減少月份數
(std::chrono::month 的公開成員函式) [編輯]