Namespaces
Variants

C++ attribute: indeterminate (since C++26)

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous
Attributes
(C++23)
(C++11) (until C++26)
(C++14)
indeterminate
(C++26)
(C++20)
(C++17)
(C++11)
(C++20)

表示如果未初始化,该变量或函数参数将具有不确定的值。

目录

语法

[ [ indeterminate ] ]

说明

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

如果函数参数声明带有 [[ indeterminate ]] 属性,则必须在其函数的首次声明中声明。若函数参数在某个翻译单元的首次函数声明中带有 [[ indeterminate ]] 属性,而同一函数在另一翻译单元的首次声明中对相同参数未使用 [[ indeterminate ]] 属性,则程序 格式错误,不要求诊断

注释

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

示例

void f(int);
void g()
{
    int x [[indeterminate]]; // 不确定值
    int y;                   // 错误值
    f(x); // 未定义行为
    f(y); // 错误行为
}
struct T
{
    T() {}
    int x;
};
void h(T a [[indeterminate]], T b)
{
    f(a.x); // 下方调用时产生未定义行为
    f(b.x); // 下方调用时产生错误行为
}
h(T(), T());

参考文献

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