std::chrono::is_clock(3)
π 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::repT::periodT::durationT::time_point
- And each of these expressions has to be well-formed when it's treated as an unevaluated operand:
T::is_steadyT::now()
π¨ Okay so real talk: if your program adds specializations forstd::is_clockorstd::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βtrueifTsatisfies the Clock requirements,falseotherwise.[static](public static member constant)
π Member functions
operator boolβ converts the object tobool, returnsvalue(public member function)operator()β returnsvalue(C++14) (public member function)
π Member types
value_typeβ defined asbooltypeβ defined asstd::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
- 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