GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::chrono::duration::operator=(3) C++ Standard Libary std::chrono::duration::operator=(3)

std::chrono::duration::operator= - std::chrono::duration::operator=


duration& operator=( const duration &other ) = default; (since C++11)


Assigns the contents of one duration to another.


other - duration to copy from

// Run this code


#include <iostream>
#include <chrono>


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';
}


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


constructor constructs new duration
(public member function)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.