跳转至

ev-abseil-constinit-macro-002

字段 值
项目 abseil/abseil-cpp
固定 commit 2065f4ded055
路径 absl/base/attributes.h
行段 L842-L847
特性 consteval 与 constinit
许可证 Apache-2.0
代码来源 项目源码
级别 manual
审核 zyuhao7 @ 2026-10-26
测试代码 否
发现方式 manual

用法

用特性测试宏把兼容宏映射到标准 constinit

为什么典型

库需要同时支持 C++17 与 C++20;可用时直接使用 constinit,保证静态存储期变量没有动态初始化。

摘录

// For code or headers that are assured to only build with C++20 and up, prefer
// just using the standard `constinit` keyword directly over this macro.
//
// Note that this attribute is redundant if the variable is declared constexpr.
#if defined(__cpp_constinit) && __cpp_constinit >= 201907L
#define ABSL_CONST_INIT constinit

备注

生产路径;注释同时说明 constexpr 变量不需要重复标注 constinit。

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