Skip to content

Commit eb49644

Browse files
committed
C++26 : 「static_assertの診断メッセージにユーザーが生成した文字列の指定を許可」を追加 (close #1177)
1 parent bec6a53 commit eb49644

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

implementation-status.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@
302302
| P2361R6: [コンパイル時にのみ使用される文字列の扱いを明確化](/lang/cpp26/unevaluated_strings.md.nolink) | `static_assert``[[deprecated]]`などで使用されるコンパイル時の文字列について、文字コードの指定を禁止し、実行時エンコーディングが行われないことを規定 | 14 | 18 | | |
303303
| P2552R3: [属性の無視性を見直し](/lang/cpp26/on_the_ignorability_of_standard_attributes.md.nolink) | 構文として適格な属性のみを無視できるようにし、そうでない属性の使用を不適格とする | | | | |
304304
| P2738R1: [定数式での`void*`からポインタ型へのキャストを許可](/lang/cpp26/constexpr_cast_from_voidptr.md.nolink) | 型消去のために`void*`からポインタ型へのキャストを許可する | 14 | 17 | | |
305-
| P2741R3: [`static_assert`の診断メッセージにユーザーが生成した文字列の指定を許可](/lang/cpp26/user-generated_static_assert_messages.md.nolink) | `constexpr``S.size()``S.data()`メンバ関数をもつオブジェクトをコンパイル時文字列として指定できるようにする | 14 | 17 | | |
305+
| P2741R3: [`static_assert`の診断メッセージにユーザーが生成した文字列の指定を許可](/lang/cpp26/user-generated_static_assert_messages.md) | `constexpr``S.size()``S.data()`メンバ関数をもつオブジェクトをコンパイル時文字列として指定できるようにする | 14 | 17 | | |
306306
| P2558R2: [基本文字集合に@、$、\`を追加](/lang/cpp26/add_atsign_dollar_graveaccent_to_the_basic_character_set.md.nolink) | C言語との互換性のためにこれらの文字を基本文字集合に追加 | | Yes | | |
307307
| P2662R3: [パラメータパックへのインデックスアクセスを許可](/lang/cpp26/pack_indexing.md.nolink) | 可変引数テンプレートのパラメータパックに添字アクセスできるようにする | | | | |
308308
| P2864R2: [非推奨となっていた列挙値から算術型への暗黙変換を削除](/lang/cpp26/remove_deprecated_arithmetic_conversion_on_enumerations.md.nolink) | C++20から非推奨となっていた列挙値への算術演算で算術型に暗黙変換される仕様を削除 | 14 | 18 | | |

lang/cpp11/static_assert.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Boost Static Assertion Libraryが開発されたときに、コンパイル時
8282
- [C++17 `static_assert`のメッセージ省略を許可](/lang/cpp17/extending_static_assert.md)
8383
- [C++17 constexpr if 文](/lang/cpp17/if_constexpr.md)
8484
- [C++23 定数式の文脈での`bool`への縮小変換を許可](/lang/cpp23/narrowing_contextual_conversions_to_bool.md)
85+
- [C++26 `static_assert`の診断メッセージにユーザーが生成した文字列の指定を許可](/lang/cpp26/user-generated_static_assert_messages.md)
8586
8687
8788
## 参照

lang/cpp26.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ C++26とは、2026年中に改訂される予定の、C++バージョンの通
4343
| 言語機能 | 説明 |
4444
|----------|------|
4545
| [定数式での`void*`からポインタ型へのキャストを許可](/lang/cpp26/constexpr_cast_from_voidptr.md.nolink) | 型消去のために`void*`からポインタ型へのキャストを許可する |
46-
| [`static_assert`の診断メッセージにユーザーが生成した文字列の指定を許可](/lang/cpp26/user-generated_static_assert_messages.md.nolink) | `constexpr``S.size()``S.data()`メンバ関数をもつオブジェクトをコンパイル時文字列として指定できるようにする |
46+
| [`static_assert`の診断メッセージにユーザーが生成した文字列の指定を許可](/lang/cpp26/user-generated_static_assert_messages.md) | `constexpr``S.size()``S.data()`メンバ関数をもつオブジェクトをコンパイル時文字列として指定できるようにする |
4747

4848

4949
### ソースコード
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# static_assertの診断メッセージにユーザーが生成した文字列の指定を許可
2+
* cpp26[meta cpp]
3+
4+
## 概要
5+
C++26では、[`static_assert`](/lang/cpp11/static_assert.md)に指定する表明失敗時の診断メッセージとして、文字列リテラルだけでなくユーザー定義型の文字列オブジェクトも指定できるようになる。
6+
7+
例として、[`std::format()`](/reference/format/format.md)関数によって作られた[`std::string`](/reference/string/basic_string.md)オブジェクトも診断メッセージとして使用できる。
8+
9+
```cpp example
10+
#include <cstdint>
11+
#include <format>
12+
13+
template <class T>
14+
void f()
15+
{
16+
static_assert(
17+
sizeof(T) == 4,
18+
std::format("type T size should 4, actual:{}", sizeof(T))
19+
);
20+
}
21+
22+
int main()
23+
{
24+
f<std::int32_t>(); // OK
25+
f<std::int64_t>(); // Error! "type T size should 4, actual:8"
26+
}
27+
```
28+
29+
30+
## 仕様
31+
`static_assert`の構文において、
32+
33+
```
34+
static_assert(定数式, 診断メッセージ);
35+
```
36+
37+
診断メッセージが文字列リテラルでない場合、そのオブジェクトMは、以下の要件を満たすこと:
38+
39+
- `M.size()`から[`std::size_t`](/reference/cstddef/size_t.md)への暗黙変換が定数式であること
40+
- `M.data()`から「`const char`へのポインタ」への暗黙変換が定数式であること
41+
- メッセージのシーケンスに含まれる各文字が、エンコーディング指定されていない文字コード (ordinary literal encoding) であること
42+
- `u8"message"`はNG
43+
- `"message"`はOK
44+
45+
46+
## 関連項目
47+
- [C++11 コンパイル時アサート](/lang/cpp11/static_assert.md)
48+
49+
50+
## 参照
51+
- [P2741R3 user-generated `static_assert` messages](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2741r3.pdf)

0 commit comments

Comments
 (0)