std::chrono::is_clock(3)

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

πŸ’… std::chrono::is_clock(3) β€” the C++ Standard Library, but make it cute

Okay so bestie, this is the std::chrono::is_clock page from the C++ Standard Library, and it's genuinely one of those tiny traits that does exactly one thing and does it iconically. Let's get into it 🫢


✨ NAME ✨

std::chrono::is_clock β€” literally just std::chrono::is_clock. That's the name. She's self-titled, main character energy, no notes πŸ’…


🌸 SYNOPSIS 🌸

Defined in header <chrono>:

template< class T >         (since C++20)
struct is_clock;

So here's the vibe: if T satisfies the Clock requirements, this trait hands you a member constant value equal to true. For any other type? value is false. Periodt. ✨

Now for the fine print, and listen up because this part actually matters: for the purpose of this trait, how far an implementation goes to determine that a type can't meet the Clock requirements is unspecified β€” with one exception: a minimum T shall not qualify as a Clock unless it meets all of the following conditions:

  • Each of these qualified identifiers has to be valid and has to denote a type:
    • T::rep
    • T::period
    • T::duration
    • T::time_point
  • And each of these expressions has to be well-formed when it's treated as an unevaluated operand:
    • T::is_steady
    • T::now()
🚨 Okay so real talk: if your program adds specializations for std::is_clock or std::is_clock_v, the behavior is undefined. Like, don't. Just don't do it. That's the whole warning. 😭

πŸ¦‹ Template parameters πŸ¦‹

  • T β€” a type to check πŸ‘€

πŸŽ€ Helper variable template πŸŽ€

template< class T >                                     (since C++20)
inline constexpr bool is_clock_v = is_clock<T>::value;

The little _v shortcut bestie. We love a convenience alias. πŸ’–


🌈 Inherited from std::integral_constant 🌈

She inherits, she doesn't reinvent. Iconic behavior honestly.

⭐ Member constants

  • value β€” true if T satisfies the Clock requirements, false otherwise. [static] (public static member constant)

πŸ’œ Member functions

  • operator bool β€” converts the object to bool, returns value (public member function)
  • operator() β€” returns value (C++14) (public member function)

πŸ“ Member types

  • value_type β€” defined as bool
  • type β€” defined as std::integral_constant<bool, value>

πŸ”₯ Possible implementation πŸ”₯

Okay this is the fun part, here's roughly how someone could build it:

template<class>
struct is_clock : std::false_type {};

template<class T>
    requires
        requires
        {
            typename T::rep;
            typename T::period;
            typename T::duration;
            typename T::time_point;
            T::is_steady; // not required to be bool
            T::now();     // not required to be T::time_point
        }
struct is_clock<T> : std::true_type {};

See those comments? T::is_steady is not required to be bool, and T::now() is not required to be T::time_point. The trait is just checking the expressions are well-formed, not that they're the "right" types. Sneaky but valid 🧚


πŸ’€ Example πŸ’€

// Run this code

#include <chrono>
#include <ratio>

int main()
{
    static_assert
    (
        std::chrono::is_clock_v<std::chrono::utc_clock>
        and
        not std::chrono::is_clock_v<std::chrono::duration<int, std::exa>>
    );
}

utc_clock? A clock, slay βœ…. duration<int, std::exa>? Not a clock, sorry babe ❌. Both asserted at compile time, no runtime drama. πŸ‘‘


🧚 Sign-off 🧚

That's the whole page, bestie β€” sourced from http://cppreference.com, dated 2024.06.10, and filed under std::chrono::is_clock(3). Go forth and check your clocks at compile time, fr fr πŸ’–βœ¨πŸŽ€

πŸ“¦ where this page comes from