std::chrono::duration<Rep,Period>::operator=

From cppreference.com
< cpp‎ | chrono‎ | duration
 
 
Utilities library
Language support
Type support (basic types, RTTI)
Library feature-test macros (哋它亢++20)
Dynamic memory management
Program utilities
Coroutine support (哋它亢++20)
Variadic functions
(哋它亢++20)
(哋它亢++26)
(哋它亢++11)
(哋它亢++20)
Debugging support
(哋它亢++26)
(哋它亢++26)
Three-way comparison
(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)
General utilities
Date and time
Function objects
Formatting library (哋它亢++20)
(哋它亢++11)
Relational operators (deprecated in 哋它亢++20)
Integer comparison functions
(哋它亢++20)(哋它亢++20)(哋它亢++20)   
(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
Swap and type operations
(哋它亢++20)
(哋它亢++14)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++23)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
Common vocabulary types
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++11)
(哋它亢++11)
(哋它亢++17)
(哋它亢++17)
(哋它亢++23)
Elementary string conversions
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)


 
Date and time utilities
Time point
(哋它亢++11)
(哋它亢++20)
(哋它亢++20)
Duration
(哋它亢++11)
Clocks
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)      
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
Time of day
(哋它亢++20)(哋它亢++20)
(哋它亢++20)(哋它亢++20)
(哋它亢++20)

Calendars
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)      
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)(哋它亢++20)
Time zones
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)(哋它亢++20)(哋它亢++20)(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)

chrono I/O
(哋它亢++20)
C-style date and time
 
 
duration& operator=( const duration &other ) = default;
(since 哋它亢++11)

Assigns the contents of one duration to another.

Parameters

other - duration to copy from

Example

#include <chrono>
#include <iostream>
 
int main()
{
    using namespace std::chrono_literals;
 
    std::chrono::hours z_hours{};
    std::chrono::seconds z_seconds{};
 
    z_hours = 2h; // ok, no conversion needed
 
    z_seconds = z_hours;
    // First, the converting ctor is used to create a temporary object of `lhs`s type.
    // This ctor implicitly invokes the casting function
    // chrono::duration_cast<std::seconds>(z_hours). The resulting `rhs` rvalue
    // has the same type as `lhs`, and the `operator=` finally performs the assignment.
 
    std::cout << "hours: " << z_hours.count() << '\n';
    std::cout << "seconds: " << z_seconds.count() << '\n';
 
    z_seconds -= 42s;
 
//  z_hours = z_seconds; // compile-time error (which is good): incompatible types.
    // The library avoids the implicit cast to prevent a potential precision loss.
 
    z_hours = std::chrono::duration_cast<std::chrono::hours>(z_seconds); // ok
    z_hours = std::chrono::duration_cast<decltype(z_hours)>(z_seconds);  // ditto
 
    std::cout << "hours: " << z_hours.count() << '\n';
    std::cout << "seconds: " << z_seconds.count() << '\n';
 
    std::chrono::duration<double, std::ratio<3600>> z2_hours{};
 
    z2_hours = z_seconds; // ok, no truncation, implicit cast
 
    std::cout << "hours: " << z2_hours.count() << '\n';
}

Output:

hours: 2
seconds: 7200
hours: 1
seconds: 7158
hours: 1.98833

See also

constructs new duration
(public member function)