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