std::litera...perator""ns(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

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:

  1. Integer literal β€” returns exactly std::chrono::nanoseconds(nsec).
  2. 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, or
  • using 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 of std::chrono::duration<Rep,Period>)

source: http://cppreference.com β€” version/date: 2024.06.10 β€” bye bestie, go count some nanoseconds πŸ’–βœ¨

πŸ“¦ where this page comes from