命名空间
变体
操作

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

来自 cppreference.cn
< 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 之间的年份差。

目录

[编辑] 返回值

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))

[编辑] 注释

如果 (1-3) 的结果年份值超出范围 [-3276732767],则存储的实际值是未指定的。

减去两个 year 值的结果是类型为 std::chrono::years 的持续时间。此持续时间单位表示平均公历年的长度,并且结果持续时间与操作数表示的特定年份中的天数无关。例如,2018y - 2017y 的结果是 std::chrono::years(1),它表示 365.2425 天,而不是 365 天。

[编辑] 示例

#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));
}

[编辑] 参见

递增或递减月份
(std::chrono::month 的公共成员函数) [编辑]
添加或减去数个月份
(std::chrono::month 的公共成员函数) [编辑]