命名空间
变体
操作

C++ 属性: indeterminate (自 C++26 起)

来自 cppreference.cn
< cpp‎ | 语言‎ | 属性
 
 
C++ 语言
通用主题
流程控制
条件执行语句
if
迭代语句(循环)
for
range-for (C++11)
跳转语句
函数
函数声明
Lambda 函数表达式
inline 说明符
动态异常规范 (直到 C++17*)
noexcept 说明符 (C++11)
异常
命名空间
类型
说明符
const/volatile
decltype (C++11)
auto (C++11)
constexpr (C++11)
consteval (C++20)
constinit (C++20)
存储期说明符
初始化
 
 
属性
(C++23)
(C++11)(直至 C++26)
(C++14)
(C++17)
indeterminate
(C++26)
(C++20)
(C++17)
(C++17)
(C++11)
(C++20)
 

指示变量或函数参数在未初始化时具有不确定值。

目录

[编辑] 语法

[[indeterminate]]

[编辑] 解释

[[indeterminate]] 可应用于具有自动存储期的块变量定义或函数声明的参数声明。该属性指定具有自动存储期的对象的存储字节最初是不确定的,而不是错误的。

如果函数参数声明带有 [[indeterminate]],则必须在其函数的首次声明中声明。如果函数参数在其函数在某个翻译单元中的首次声明中声明带有 [[indeterminate]],而同一函数在另一个翻译单元中的首次声明中在同一参数上未声明 [[indeterminate]],则程序格式错误,无需诊断

[编辑] 注意

[[indeterminate]] 属性恢复了直到 C++26 才隐式引入的未定义行为。它可能会使编译器将读取不确定值的代码路径视为不可达。

[编辑] 示例

void f(int);
 
void g()
{
    int x [[indeterminate]]; // indeterminate value
    int y;                   // erroneous value
 
    f(x); // undefined behavior
    f(y); // erroneous behavior
}
 
struct T
{
    T() {}
    int x;
};
 
void h(T a [[indeterminate]], T b)
{
    f(a.x); // undefined behavior when called below
    f(b.x); // erroneous behavior when called below
}
 
h(T(), T());

[编辑] 参考

  • C++26 标准 (ISO/IEC 14882:2026)
  • 9.12.7 不确定存储 [dcl.attr.indet]