Skip to content

Commit 45cad45

Browse files
committed
C++23 : 「定数式の文脈でのboolへの縮小変換を許可」を追加 (close #1037)
1 parent e24da03 commit 45cad45

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

implementation-status.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@
266266
| P2096R2: [部分特殊化の汎用化仕様](/lang/cpp23/generalized_wording_for_partial_specializations.md.nolink) | 変数テンプレートの部分特殊化を許可するために部分特殊化の仕様を汎用化 | - | - | - | - |
267267
| P2582R1: [継承コンストラクタからのクラステンプレート引数の推論](/lang/cpp23/class_template_argument_deduction_from_inherited.md) | 継承コンストラクタからもクラステンプレート引数を推論できるようにする | - | - | - | - |
268268
| P1938R3: [`if consteval`](/lang/cpp23/if_consteval.md.nolink) | コンパイル時の文脈かどうかで分岐させる | 12 | 14 | - | - |
269-
| P1401R5: [定数式の文脈での`bool`への縮小変換を許可](/lang/cpp23/narrowing_contextual_conversions_to_bool.md.nolink) | `if constexpr(flags & Flags::Exec)``static_assert(N);`を許可 | 9 | 13 | 2022.2 | - |
269+
| P1401R5: [定数式の文脈での`bool`への縮小変換を許可](/lang/cpp23/narrowing_contextual_conversions_to_bool.md) | `if constexpr(flags & Flags::Exec)``static_assert(N);`を許可 | 9 | 13 | 2022.2 | - |
270270
| P2242R3: [定数式内での非リテラル変数、静的変数・スレッドローカル変数およびgotoとラベルの存在を許可する](/lang/cpp23/non_literal_variables_in_constexpr_functions.md) | コンパイル時に評価されない限り、定数式内に静的変数・スレッドローカル変数およびgoto文とラベルを含むことを許可する | 12 | 15 | 2022.2 | - |
271271
| P2246R1: [静的な診断メッセージの文字エンコーディング](/lang/cpp23/character_encoding_of_diagnostic_text.md) | `static_assert``[[deprecated]]`などの診断メッセージの文字集合に関する要件をなくす | - | yes | - | 2022 |
272272
| P2448R2: [`constexpr`関数が定数実行できない場合でも適格とする](/lang/cpp23/relaxing_some_constexpr_restrictions.md) | | 13 | 17 (partial) | 2024.0 (partial) | - |

lang/cpp11/static_assert.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ Boost Static Assertion Libraryが開発されたときに、コンパイル時
7878
7979
8080
## 関連項目
81-
- [C++17 `static_assert`](/lang/cpp17/extending_static_assert.md) — 第2引数の診断メッセージを省略できるようになった
82-
81+
- [C++17 `static_assert`のメッセージ省略を許可](/lang/cpp17/extending_static_assert.md)
82+
- [C++23 定数式の文脈での`bool`への縮小変換を許可](/lang/cpp23/narrowing_contextual_conversions_to_bool.md)
8383
8484
8585
## 参照

lang/cpp17/if_constexpr.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ template <int arg, typename ... Args> int do_something(Args... args) {
412412
## 関連項目
413413

414414
- [`std::conditional`](/reference/type_traits/conditional.md)
415+
- [C++23 定数式の文脈での`bool`への縮小変換を許可](/lang/cpp23/narrowing_contextual_conversions_to_bool.md)
415416

416417
## 参照
417418

lang/cpp23.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ C++23とは、2023年中に改訂される予定の、C++バージョンの通
6868
| 言語機能 | 説明 |
6969
|----------|------|
7070
| [`if consteval`](cpp23/if_consteval.md.nolink) | コンパイル時の文脈かどうかで分岐させる |
71-
| [定数式の文脈での`bool`への縮小変換を許可](cpp23/narrowing_contextual_conversions_to_bool.md.nolink) | `if constexpr(flags & Flags::Exec)``static_assert(N);`を許可 |
71+
| [定数式の文脈での`bool`への縮小変換を許可](cpp23/narrowing_contextual_conversions_to_bool.md) | `if constexpr(flags & Flags::Exec)``static_assert(N);`を許可 |
7272
| [定数式内での非リテラル変数、静的変数・スレッドローカル変数およびgotoとラベルの存在を許可する](cpp23/non_literal_variables_in_constexpr_functions.md) | コンパイル時に評価されない限り、定数式内に静的変数・スレッドローカル変数およびgoto文とラベルを含むことを許可する |
7373
| [静的な診断メッセージの文字エンコーディング](cpp23/character_encoding_of_diagnostic_text.md) | `static_assert``[[deprecated]]`などの診断メッセージの文字集合に関する要件をなくす |
7474
| [`constexpr`関数が定数実行できない場合でも適格とする](cpp23/relaxing_some_constexpr_restrictions.md) | 定数式実行できない関数であっても、実際にコンパイル時に評価されない限り`constexpr`指定することを許可する |
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 定数式の文脈でのboolへの縮小変換を許可
2+
* cpp23[meta cpp]
3+
4+
## 概要
5+
C++23では、以下の定数式文脈での`bool`への縮小変換を許可する:
6+
7+
- `static_assert`
8+
- `if constexpr`
9+
10+
これにより、整数値から`bool`への変換 (ビットフラグの判定などで使われる)、スコープをもたない列挙値型から`bool`への変換ができる定数式の条件式内でできるようになる。
11+
12+
ただし`explicit(bool)`および`noexcept(bool)`のパラメータについては縮小変換が許可されないため、整数値として値`1``0`のどちらかしか指定できず、`noexcept(sizeof(char[2]))`のようなコードはコンパイルエラーとなる。
13+
14+
15+
### `static_assert`の例
16+
17+
```cpp
18+
// 整数値が0でなければtrue、そうでなければfalse
19+
static_assert(bool(N)); // これまでの書き方
20+
static_assert(N); // C++23以降
21+
22+
static_assert(sizeof(int[2])); // C++23:OK 縮小変換
23+
```
24+
25+
### `if constexpr`の例
26+
```cpp
27+
if constexpr(bool(flags & Flags::Exec)) { … } // これまでの書き方
28+
if constexpr((flags & Flags::Exec) != 0) { … } // これまでの書き方
29+
if constexpr(flags & Flags::Exec) { … } // C++23以降
30+
31+
if constexpr (sizeof(int[2])) { … } // C++23:OK 縮小変換
32+
```
33+
34+
## 関連項目
35+
- [C++11 コンパイル時アサート](/lang/cpp11/static_assert.md)
36+
- [C++17 `constexpr if`文](/lang/cpp17/if_constexpr.md)
37+
38+
39+
## 参照
40+
- [P1401R5 Narrowing contextual conversions to `bool`](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p1401r5.html)

0 commit comments

Comments
 (0)