Skip to content

Commit eb61e1a

Browse files
committed
C++26: 「constexpr構造化束縛の許可と、constexpr参照の制限緩和」を追加 #1372
1 parent 70977b4 commit eb61e1a

5 files changed

Lines changed: 60 additions & 2 deletions

File tree

implementation-status.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@
313313
| P3144R2: [不完全型へのポインタに対する`delete`を不適格とする](/lang/cpp26/deleting_a_pointer_to_an_incomplete_type_should_be_ill-formed.md) | 未定義動作を引き起こす操作をコンパイルエラーとする | 15 | 19 | | |
314314
| P2963R3: [制約式内での畳み込み式の順序付け](/lang/cpp26/ordering_of_constraints_involving_fold_expressions.md.nolink) | 畳み込み式では全体ではなく個別の制約を原子制約式として扱う | | 19 | | |
315315
| P0963R3: [条件式での構造化束縛の使用を許可](/lang/cpp26/structured_binding_declaration_as_a_condition.md) | 式全体を`bool`値に変換できる場合に条件式で構造化束縛を使用できることとする | 15 | | | |
316-
| P2686R5: [`constexpr`構造化束縛の許可と、定数式への参照を定数式とする](/lang/cpp26/constexpr_structured_bindings_and_references_to_constexpr_variables.md.nolink) | 定数式に対する構造化束縛を許可し、関連する定数式への参照が定数式になるようにする | | | | |
316+
| P2686R5: [`constexpr`構造化束縛の許可と、`constexpr`参照の制限緩和](/lang/cpp26/constexpr_structured_bindings_and_references_to_constexpr_variables.md) | 定数式に対する構造化束縛を許可し、関連して`constexpr`参照がの制限を緩和して自動変数も参照できるようにする | | | | |
317317
| P3068R6: [定数評価での例外送出を許可](/lang/cpp26/allowing_exception_throwing_in_constant-evaluation.md) | 定数式の文脈での例外の送出と捕捉を許可 | | | | |
318318
| P2865R6: [非推奨だった組み込み配列の比較を削除](/lang/cpp26/remove_deprecated_array_comparisons.md) | C++20で非推奨となっていた配列比較を削除 | 15 | 20 | | |
319319
| P1061R10: [構造化束縛でパックを導入できるようにする](/lang/cpp26/structured_bindings_can_introduce_a_pack.md) | タプルを分解する際に複数の変数をパックとして宣言できるようにする。`auto [a, ...xs] = f();` | | | | |

lang/cpp11/constexpr.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ GCC 5.2、Clang 3.7、Visual C++ 2015時点で、3つともデフォルトは512
218218
- [C++26 定数評価での例外送出を許可](/lang/cpp26/allowing_exception_throwing_in_constant-evaluation.md)
219219
- [C++26 定数式での`void*`からポインタ型へのキャストを許可](/lang/cpp26/constexpr_cast_from_voidptr.md)
220220
- [C++26 `constexpr`配置`new`](/lang/cpp26/constexpr_placement_new.md)
221+
- [C++26 `constexpr`構造化束縛の許可と、`constexpr`参照の制限緩和](/lang/cpp26/constexpr_structured_bindings_and_references_to_constexpr_variables.md)
221222
222223
223224
## 参照

lang/cpp17/structured_bindings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ auto { w, {x, y}, z } = f(); // このような、tuple内にあるpairを同時
343343
- [C++26 条件式での構造化束縛の使用を許可](/lang/cpp26/structured_binding_declaration_as_a_condition.md)
344344
- [C++26 宣言のみで使用しない変数の名前として`_`をサポート](/lang/cpp26/nice_placeholder_with_no_name.md)
345345
- [C++26 構造化束縛でパックを導入できるようにする](/lang/cpp26/structured_bindings_can_introduce_a_pack.md)
346+
- [C++26 `constexpr`構造化束縛の許可と、`constexpr`参照の制限緩和](/lang/cpp26/constexpr_structured_bindings_and_references_to_constexpr_variables.md)
346347
347348
348349
## 参照

lang/cpp26.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ C++26とは、2026年中に改訂される予定の、C++バージョンの通
7878
| [定数式での`void*`からポインタ型へのキャストを許可](/lang/cpp26/constexpr_cast_from_voidptr.md) | 型消去のために`void*`からポインタ型へのキャストを許可する |
7979
| [`static_assert`の診断メッセージにユーザーが生成した文字列の指定を許可](/lang/cpp26/user-generated_static_assert_messages.md) | `constexpr``S.size()``S.data()`メンバ関数をもつオブジェクトをコンパイル時文字列として指定できるようにする |
8080
| [`constexpr`配置`new`](/lang/cpp26/constexpr_placement_new.md) | 定数式の文脈での配置`new`を許可 |
81-
| [`constexpr`構造化束縛の許可と、定数式への参照を定数式とする](/lang/cpp26/constexpr_structured_bindings_and_references_to_constexpr_variables.md.nolink) | 定数式に対する構造化束縛を許可し、関連する定数式への参照が定数式になるようにする |
81+
| [`constexpr`構造化束縛の許可と、`constexpr`参照の制限緩和](/lang/cpp26/constexpr_structured_bindings_and_references_to_constexpr_variables.md) | 定数式に対する構造化束縛を許可し、関連して`constexpr`参照がの制限を緩和して自動変数も参照できるようにする |
8282
| [定数評価での例外送出を許可](/lang/cpp26/allowing_exception_throwing_in_constant-evaluation.md) | 定数式の文脈での例外の送出と捕捉を許可 |
8383

8484

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# constexpr構造化束縛の許可と、constexpr参照の制限緩和 [P2686R5]
2+
* cpp26[meta cpp]
3+
4+
<!-- start lang caution -->
5+
6+
このページはC++26に採用される見込みの言語機能の変更を解説しています。
7+
8+
のちのC++規格でさらに変更される場合があるため[関連項目](#relative-page)を参照してください。
9+
10+
<!-- last lang caution -->
11+
12+
## 概要
13+
C++26では、定数式の文脈での構造化束縛が許可されるようになり、`constexpr`変数への参照が定数式として扱われるようになる。
14+
15+
### `constexpr`構造化束縛の許可
16+
17+
C++23までは構造化束縛を`constexpr`として使用することはできなかったが、C++26では以下のようなコードが許可される。
18+
19+
```cpp
20+
constexpr std::tuple<int, int> p{1, 2};
21+
constexpr auto [x, y] = p; // C++26: OK
22+
```
23+
24+
構造化束縛は以下のように参照に展開されるため、`constexpr`参照と同じ制限を受ける:
25+
26+
```cpp
27+
void f() {
28+
constexpr std::tuple<int, int> p{1, 2};
29+
constexpr const auto& x = get<0>(p);
30+
constexpr const auto& y = get<1>(p);
31+
}
32+
```
33+
* get[link /reference/tuple/tuple/get.md]
34+
35+
36+
### `constexpr`変数への参照の制限緩和
37+
`constexpr`参照はC++23まで、静的記憶域をもつ変数への参照のみ許可されていた (テンプレートパラメータの`template <auto& r>`も同様)。C++26ではその制限が緩和され、自動記憶域 (ブロックスコープ) をもつ変数でも`constexpr`参照ができるようになる。
38+
39+
```cpp
40+
void f() {
41+
constexpr int a = 42;
42+
constexpr const int& ref = a; // C++23: NG, C++26: OK
43+
}
44+
```
45+
46+
ただし`constexpr`参照の制限として、参照する変数のアドレスが、参照または構造化束縛が存在するスタックフレームを基準とした定数である場合に限られる。
47+
48+
49+
## <a id="relative-page" href="#relative-page">関連項目</a>
50+
- [C++11 constexpr](/lang/cpp11/constexpr.md)
51+
- [C++17 構造化束縛](/lang/cpp17/structured_bindings.md)
52+
53+
54+
## 参照
55+
- [P2686R5 `constexpr` structured bindings and references to `constexpr` variables](https://open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2686r5.pdf)
56+
- [Trip report: November 2024 ISO C++ standards meeting (Wrocław, Poland) - Sutter's Mill](https://herbsutter.com/2024/11/24/wg21-2024-11/)

0 commit comments

Comments
 (0)