function template
<chrono>

std::chrono::duration operators

member functions
/* member functions: */
duration& operator= (const duration& rhs) = default;
constexpr duration operator+() const;
constexpr duration operator-() const;
duration& operator++();
duration  operator++(int);
duration& operator--();
duration  operator--(int);
duration& operator+= (const duration& rhs);
duration& operator-= (const duration& rhs);
duration& operator*= (const rep& r);
duration& operator/= (const rep& r);
duration& operator%= (const rep& r);
duration& operator%= (const duration& rhs);
non-member functions

/* non-member functions: */
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr typename common_type<duration<Rep1,Period1>,duration<Rep2,Period2>>::type
  operator+ (const duration<Rep1,Period1>& lhs, const duration<Rep2,Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr typename common_type<duration<Rep1,Period1>,duration<Rep2,Period2>>::type
  operator- (const duration<Rep1,Period1>& lhs, const duration<Rep2,Period2>& rhs);
template <class Rep1, class Period, class Rep2>
constexpr duration<typename common_type<Rep1,Rep2>::type, Period>
  operator* (const duration<Rep1,Period>& lhs, const Rep2& r);
template <class Rep1, class Rep2, class Period>
constexpr duration<typename common_type<Rep1,Rep2>::type, Period>
  operator* (const Rep1& s, const duration<Rep2,Period>& rhs);
template <class Rep1, class Period, class Rep2>
constexpr duration<typename common_type<Rep1,Rep2>::type, Period>
  operator/ (const duration<Rep1,Period>& lhs, const Rep2& r);
template <class Rep1, class Rep2, class Period>
constexpr duration<typename common_type<Rep1,Rep2>::type, Period>
  operator/ (const Rep1& s, const duration<Rep2,Period>& rhs);
template <class Rep1, class Period, class Rep2>
constexpr duration<typename common_type<Rep1,Rep2>::type, Period>
  operator% (const duration<Rep1,Period>& lhs, const Rep2& r);
template <class Rep1, class Rep2, class Period>
constexpr duration<typename common_type<Rep1,Rep2>::type, Period>
  operator% (const Rep1& s, const duration<Rep2,Period>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator== (const duration<Rep1,Period1>& lhs, const duration<Rep2,Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator!= (const duration<Rep1,Period1>& lhs, const duration<Rep2,Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator< (const duration<Rep1,Period1>& lhs, const duration<Rep2,Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator> (const duration<Rep1,Period1>& lhs, const duration<Rep2,Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator>= (const duration<Rep1,Period1>& lhs, const duration<Rep2,Period2>& rhs);
template <class Rep1, class Period1, class Rep2, class Period2>
constexpr bool operator<= (const duration<Rep1,Period1>& lhs, const duration<Rep2,Period2>& rhs);
Duration operators
Performs the appropriate operation on the duration objects involved as if it was applied directly on its iternal count object.

When two duration objects of different types are involved, the one with the longest period (as determined by common_type) is converted before the operation.

Parameters

lhs,rhs
duration objects (to the left- and right-hand side of the operator, respectively).
s,r
A value of an arithmetic type, or an object of a class emulating an arithmetic type.

Return value

The result of the operation.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// duration operators
#include <iostream>
#include <ratio>
#include <chrono>

int main ()
{

  std::chrono::duration<int> foo;
  std::chrono::duration<int> bar (10);

                    // counts: foo bar
                    //         --- ---
  foo = bar;                 // 10  10
  foo = foo + bar;           // 20  10
  ++foo;                     // 21  10
  --bar;                     // 21   9
  foo *= 2;                  // 42   9
  foo /= 3;                  // 14   9
  bar += ( foo % bar );      // 14  14

  std::cout << std::boolalpha;
  std::cout << "foo==bar: " << (foo==bar) << std::endl;
  std::cout << "foo: " << foo.count() << std::endl;
  std::cout << "bar: " << bar.count() << std::endl;

  return 0;
}


Output:
foo==bar: true
foo: 14
bar: 14

See also