Namespaces
Variants

std::chrono::duration<Rep,Period>:: operator+ (unary) , std::chrono::duration<Rep,Period>:: operator- (unary)

From cppreference.net
(1)
constexpr duration operator + ( ) const ;
(until C++17)
constexpr std:: common_type_t < duration > operator + ( ) const ;
(since C++17)
(2)
constexpr duration operator - ( ) const ;
(until C++17)
constexpr std:: common_type_t < duration > operator - ( ) const ;
(since C++17)

为时间段实现一元加法和一元减法。

如果 rep_ 是用于存储时长对象中滴答次数的成员变量,且 D 是返回类型,

1) 等价于 return D ( * this ) ;
2) 等价于 return D ( - rep_ ) ;

目录

参数

(无)

返回值

1) 该时长对象的一个副本。
2) 该时长对象的副本,其滴答计数为取负值。

示例

#include <chrono>
#include <iostream>
int main()
{
    constexpr std::chrono::seconds s1(-052);
    constexpr std::chrono::seconds s2 = -s1;
    std::cout << "Negated " << s1 << " are " << s2 << '\n';
}

输出:

Negated -42s are 42s

参见

递增或递减滴答计数
(公开成员函数)
实现以时长为参数的算术运算
(函数模板)