std::chrono...::operator=(3)
π NAME π
okay so this is the std::chrono...::operator=(3) page from the C++ Standard Libary manual, and the NAME is literally:
std::chrono::duration::operator=βstd::chrono::duration::operator=
β¨ Synopsis β¨
Bestie, hereβs the whole synopsis vibe:
duration& operator=( const duration &other ) = default; (since C++11)
Assigns the contents of one duration to another. Thatβs it. Thatβs the assignment moment. π
πΈ Parameters πΈ
otherβ duration to copy from
π Example π
Run this code:
// Run this code
#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 π
constructorβ constructs new duration (public member function)
mwah π source/version: http://cppreference.com, date: 2024.06.10, page: std::chrono...::operator=(3) π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms