Namespaces
Variants
Views
Actions

std::chrono::duration::operator+=, -=, *=, /=, %=

From cppreference.com
< cpp‎ | chrono‎ | duration
Revision as of 16:23, 2 November 2012 by P12bot (Talk | contribs)

 
 
 
 
 

Template:ddcl list begin <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
duration& operator+=(const duration& d);
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
duration& operator-=(const duration& d);
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
duration& operator*=(const rep& rhs);
</td>

<td > (3) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
duration& operator/=(const rep& rhs);
</td>

<td > (4) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
duration& operator%=(const rep& rhs);
</td>

<td > (5) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
duration& operator%=(const duration& rhs);
</td>

<td > (6) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Performs compound assignments between two durations with the same period or between a duration and a tick count value.

If rep_ is the member variable holding the number of ticks in this duration object,

1) Equivalent to rep_ += d.count(); return *this;
2) Equivalent to rep_ -= d.count(); return *this;
3) Equivalent to rep_ *= rhs; return *this;
4) Equivalent to rep_ /= rhs; return *this;
5) Equivalent to rep_ %= rhs; return *this;
6) Equivalent to rep_ %= d.count(); return *this;

Contents

Parameters

d - duration on the right-hand side of the operator
rhs - number of ticks on the right-hand side of the operator

Return value

A reference to this duration after modification

Example

#include <chrono>
#include <iostream>
 
int main()
{
    std::chrono::minutes m(11);
    m *= 2;
    m += std::chrono::hours(10); // hours implicitly convert to minutes
    std::cout << m.count() << " minutes equals "
              << std::chrono::duration_cast<std::chrono::hours>(m).count() 
              << " hours and ";
    m %= std::chrono::hours(1);
    std::cout << m.count() << " minutes\n";
}

Output:

622 minutes equals 10 hours and 22 minutes

See also

Template:cpp/chrono/duration/dcl list operator arith2Template:cpp/chrono/duration/dcl list operator arith4