C++ 屬性: indeterminate (自 C++26 起)
來自 cppreference.com
指示變數或函式引數在未初始化時具有不確定值。
目錄 |
[編輯] 語法
[[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]