Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/language/attributes/indeterminate"

From cppreference.com
< cpp‎ | language‎ | attributes
(Added indeterminate, which is introduced by P2795R5 (Erroneous behaviour for uninitialized reads).)
 
m (Explanation: +link to IFNDR.)
Line 14: Line 14:
 
{{attr|indeterminate}} can be applied to the definition of a block variable with automatic {{lt|cpp/language/storage duration}} or to a declaration of a parameter of a [[cpp/language/function|function declaration]]. The attribute specifies that the bytes comprising the storage of an object with automatic storage duration is initially [[cpp/language/default initialization#Indeterminate and erroneous values|indeterminate]] rather than erroneous.
 
{{attr|indeterminate}} can be applied to the definition of a block variable with automatic {{lt|cpp/language/storage duration}} or to a declaration of a parameter of a [[cpp/language/function|function declaration]]. The attribute specifies that the bytes comprising the storage of an object with automatic storage duration is initially [[cpp/language/default initialization#Indeterminate and erroneous values|indeterminate]] rather than erroneous.
  
If a function parameter is declared with {{attr|indeterminate}}, it must be declared in the first declaration of its function. If a function parameter is declared with {{attr|indeterminate}} in the first declaration of its function in one translation unit and the same function is declared without {{attr|indeterminate}} on the same parameter in its first declaration in another translation unit, the program is ill-formed, no diagnostic required.
+
If a function parameter is declared with {{attr|indeterminate}}, it must be declared in the first declaration of its function. If a function parameter is declared with {{attr|indeterminate}} in the first declaration of its function in one translation unit and the same function is declared without {{attr|indeterminate}} on the same parameter in its first declaration in another translation unit, the program is [[cpp/language/ub#Explanation|ill-formed, no diagnostic required]].
  
 
===Example===
 
===Example===

Revision as of 06:14, 23 April 2024

 
 
C++ language
General topics
Flow control
Conditional execution statements
if
Iteration statements (loops)
for
range-for (C++11)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications (until C++17*)
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
const/volatile
decltype (C++11)
auto (C++11)
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
explicit (C++11)
static

Special member functions
Templates
Miscellaneous
 
 
Attributes
(C++23)
(C++14)
indeterminate
(C++26)
(C++20)
(C++17)
(C++11)
(C++20)
 

Indicates that the variable or function parameter has an indeterminate value if it is not initialized.

Syntax

[[indeterminate]]

Explanation

[[indeterminate]] can be applied to the definition of a block variable with automatic storage duration or to a declaration of a parameter of a function declaration. The attribute specifies that the bytes comprising the storage of an object with automatic storage duration is initially indeterminate rather than erroneous.

If a function parameter is declared with [[indeterminate]], it must be declared in the first declaration of its function. If a function parameter is declared with [[indeterminate]] in the first declaration of its function in one translation unit and the same function is declared without [[indeterminate]] on the same parameter in its first declaration in another translation unit, the program is ill-formed, no diagnostic required.

Example

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());