std::litera...perator""ns(3)
std::litera...perator""ns(3) π
okay so this is the C++ Standard Libary manual page for std::literals::chrono_literals::operator""ns β yes, the header spells βLibaryβ like that, and we are not fixing it, bestie. It's giving βtiny time unit literal realness,β and it's lowkey iconic. π°οΈβ¨
β¨ NAME β¨
std::literals::chrono_literals::operator""ns β and fr fr, that's the whole name: std::literals::chrono_literals::operator""ns
πΈ Synopsis
Defined in header <chrono>.
constexpr std::chrono::nanoseconds (1)
(since C++14)
operator""ns( unsigned long long nsec );
constexpr std::chrono::duration</*unspecified*/, std::nano> (2)
(since C++14)
operator""ns( long double nsec );
It forms a std::chrono::duration literal representing nanoseconds. Here's the tea:
- Integer literal β returns exactly
std::chrono::nanoseconds(nsec). - Floating-point literal β returns a floating-point duration equivalent to
std::chrono::nanoseconds.
π Parameters
nsecβ the number of nanoseconds. that's the parameter, bestie π
π¦ Return value
The std::chrono::duration literal. periodt. β¨
π₯ Possible implementation
constexpr std::chrono::nanoseconds operator""ns(unsigned long long
ns)
{
return std::chrono::nanoseconds(ns);
}
constexpr std::chrono::duration<long double, std::nano>
operator""ns(long double ns)
{
return std::chrono::duration<long double, std::nano>(ns);
}
π Notes
This operator is declared in the namespace std::literals::chrono_literals, where both literals and chrono_literals are inline namespaces. Access to this operator can be gained with:
using namespace std::literals,using namespace std::chrono_literals, orusing namespace std::literals::chrono_literals.
In addition, within the namespace std::chrono, the directive using namespace literals::chrono_literals; is provided by the standard library, so that if a programmer uses using namespace std::chrono; to gain access to the classes in the chrono library, the corresponding literal operators become visible as well. slay. π
π Example
// Run this code
#include <chrono>
#include <iostream>
int main()
{
using namespace std::chrono_literals;
auto d1{250ns};
std::chrono::nanoseconds d2{1us};
std::cout << d1 << " = " << d1.count() << " nanoseconds\n"
<< d2 << " = " << d2.count() << " nanoseconds\n";
}
β Output
250ns = 250 nanoseconds
1000ns = 1000 nanoseconds
π See also
constructorβ constructs new duration (public member function ofstd::chrono::duration<Rep,Period>)
source: http://cppreference.com β version/date: 2024.06.10 β bye bestie, go count some nanoseconds πβ¨
π¦ 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