std::chrono::is_clock
来自 cppreference.cn
| 定义于头文件 <chrono> |
||
| template< class T > struct is_clock; |
(C++20 起) | |
如果 T 满足 Clock 的要求,则提供成员常量 value 等于 true。对于任何其他类型,value 为 false。
对于此特性,实现确定类型无法满足 Clock 要求的程度是未指定的,除非最小的 T 不符合 Clock 的条件,除非它满足以下所有条件:
- 以下每个 限定标识符 都是有效的,并表示一个类型:
-
T::rep -
T::period -
T::duration -
T::time_point
-
- 以下每个表达式在被视为 未求值操作数 时都是格式良好的:
-
T::is_steady -
T::now()
-
如果程序为 std::is_clock 或 std::is_clock_v 添加特化,则行为是未定义的。
目录 |
[编辑] 模板参数
| T | - | 要检查的类型 |
[编辑] 辅助变量模板
| template< class T > constexpr bool is_clock_v = is_clock<T>::value; |
(C++20 起) | |
继承自 std::integral_constant
成员常量
| value [静态] |
如果 T 满足 Clock 要求,则为 true,否则为 false(public static 成员常量) |
成员函数
| operator bool |
将对象转换为 bool,返回 value (公开成员函数) |
| operator() (C++14) |
返回 value (公开成员函数) |
成员类型
| 类型 | 定义 |
value_type
|
bool |
类型
|
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() {}