跳转至

ev-abseil-constexpr-duration-factory-003

字段 值
项目 abseil/abseil-cpp
固定 commit 2065f4ded055
路径 absl/time/time.h
行段 L537-L540
特性 constexpr
许可证 Apache-2.0
代码来源 项目源码
级别 manual
审核 zyuhao7 @ 2026-10-27
测试代码 否
发现方式 manual

用法

用 constexpr 工厂函数在编译期构造 Duration 常量

为什么典型

时间单位换算无副作用,constexpr 让 Seconds(60) 之类的调用能用于常量初始化。

摘录

template <typename T, time_internal::EnableIfIntegral<T> = 0>
ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Nanoseconds(T n) {
  return time_internal::FromInt64(n, std::nano{});
}

备注

生产路径;时间库的单位工厂函数,配合类型约束只接受整数类型。

查看固定 commit 源码 · 返回证据索引