C++ 属性: indeterminate (自 C++26 起)
来自 cppreference.cn
指示变量或函数参数在未初始化时具有不确定值。
目录 |
[编辑] 语法
[[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]