命名空间
变体
操作

std::chrono::is_clock

来自 cppreference.cn
< cpp‎ | chrono
 
 
 
定义于头文件 <chrono>
template< class T >
struct is_clock;
(C++20 起)

如果 T 满足 Clock 的要求,则提供成员常量 value 等于 true。对于任何其他类型,valuefalse

对于此特性,实现确定类型不能满足 Clock 要求的程度是未指定的,但最低限度是,除非 T 满足以下所有条件,否则 T 不应被视为 Clock

  • T::rep
  • T::period
  • T::duration
  • T::time_point
  • T::is_steady
  • T::now()

如果程序为 std::is_clockstd::is_clock_v 添加特化,则行为未定义。

内容

[编辑] 模板形参

T - 要检查的类型

[编辑] 辅助变量模板

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

继承自 std::integral_constant

成员常量

value
[静态]
true 如果 T 满足 Clock 的要求,否则为 false
(公共静态成员常量)

成员函数

operator bool
将对象转换为 bool,返回 value
(公共成员函数)
operator()
(C++14)
返回 value
(公共成员函数)

成员类型

类型 定义
value_type bool
type std::integral_constant<bool, value>

[编辑] 可能的实现

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; // type is not checked
            T::now();     // return type is not checked
        }
struct is_clock<T> : std::true_type {};

[编辑] 注解

如果 T 满足 Clock 的其他要求,但 T::is_steady 不是 const bool 类型,或者 T::now() 不是 T::time_point 类型,则 is_clock_v<T> 的结果是未指定的。

[编辑] 示例

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