std::chrono::operator+, std::chrono::operator- (std::chrono::weekday)
来自cppreference.com
constexpr std::chrono::weekday operator+(const std::chrono::weekday& wd, const std::chrono::days& d) noexcept; |
(1) | (C++20 起) |
constexpr std::chrono::weekday operator+(const std::chrono::days& d, const std::chrono::weekday& wd) noexcept; |
(2) | (C++20 起) |
constexpr std::chrono::weekday operator-(const std::chrono::weekday& wd, const std::chrono::days& d) noexcept; |
(3) | (C++20 起) |
constexpr std::chrono::days operator-(const std::chrono::weekday& wd1, const std::chrono::weekday& wd2) noexcept; |
(4) | (C++20 起) |
1-2) 向
wd
加上 d.count()
日。通过首先求值 static_cast<long long>(wd.c_encoding()) + d.count() 再将它对 7 取余,得到范围 [0, 6] 中的整数,计算结果中保有的星期之日值。3) 从
wd
减去 d.count()
日。等价于 return wd + -d; 。4) 若 wd1.ok() 与 wd2.ok() 均为 true ,则返回 std::chrono::days 值
d
,满足 d.count()
在范围 [0, 6] 中且 wd2 + d == wd1 。否则返回值未指定。返回值
1-3) 保有按上述方式计算的星期之日值的 std::chrono::weekday 。
4) 表示
wd1
与 wd2
间距离的 std::chrono::days 。注意
只要计算不溢出, (1-3) 就始终返回合法的 weekday
,即使 wd.ok() 为 false 。
示例
运行此代码
#include <iostream> #include <chrono> int main() { std::cout << std::boolalpha; std::chrono::weekday wd {4}; wd = wd + std::chrono::days(2); std::cout << (wd == std::chrono::weekday(6)) << ' ' << (wd == std::chrono::Saturday) << ' '; wd = wd - std::chrono::days(3); std::cout << (wd == std::chrono::weekday(3)) << ' ' << (wd == std::chrono::Wednesday) << ' '; wd = std::chrono::Tuesday; wd = wd + std::chrono::days{8}; // (((2 + 8) == 10) % 7) == 3; std::cout << (wd == std::chrono::Wednesday) << ' '; wd = wd + (std::chrono::Sunday - std::chrono::Thursday); // (3 + 3) == 6 std::cout << (wd == std::chrono::Saturday) << '\n'; }
输出:
true true true true true true
参阅
自增或自减 weekday (公开成员函数) | |
加或减日数 (公开成员函数) |