Skip to content

Commit f91ef86

Browse files
committed
C++26: 「属性の無視性を見直し」を追加 (close #1175)
1 parent 02e8675 commit f91ef86

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

implementation-status.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@
295295
| P2169R4: [宣言のみで使用しない変数の名前として`_`をサポート](/lang/cpp26/nice_placeholder_with_no_name.md) | 変数名`_`は暗黙で`[[maybe_unused]]`が指定される | 14 | 18 | | |
296296
| P1854R4: [文字列リテラルの文字エンコーディング失敗を不適格とする](/lang/cpp26/making_non-encodable_string_literals_ill-formed.md) | 文字列リテラルのエンコーディング時に文字表現が失われる場合にコンパイルエラーにする | 14 | 14 | | |
297297
| P2361R6: [コンパイル時にのみ使用される文字列の扱いを明確化](/lang/cpp26/unevaluated_strings.md.nolink) | `static_assert``[[deprecated]]`などで使用されるコンパイル時の文字列について、文字コードの指定を禁止し、実行時エンコーディングが行われないことを規定 | 14 | 18 | | |
298-
| P2552R3: [属性の無視性を見直し](/lang/cpp26/on_the_ignorability_of_standard_attributes.md.nolink) | 構文として適格な属性のみを無視できるようにし、そうでない属性の使用を不適格とする | | | | |
298+
| P2552R3: [属性の無視性を見直し](/lang/cpp26/on_the_ignorability_of_standard_attributes.md) | 構文として適格な属性のみを無視できるようにし、そうでない属性の使用を不適格とする | | | | |
299299
| P2738R1: [定数式での`void*`からポインタ型へのキャストを許可](/lang/cpp26/constexpr_cast_from_voidptr.md.nolink) | 型消去のために`void*`からポインタ型へのキャストを許可する | 14 | 17 | | |
300300
| P2741R3: [`static_assert`の診断メッセージにユーザーが生成した文字列の指定を許可](/lang/cpp26/user-generated_static_assert_messages.md) | `constexpr``S.size()``S.data()`メンバ関数をもつオブジェクトをコンパイル時文字列として指定できるようにする | 14 | 17 | | |
301301
| P2558R2: [基本文字集合に@、$、\`を追加](/lang/cpp26/add_atsign_dollar_graveaccent_to_the_basic_character_set.md) | C言語との互換性のためにこれらの文字を基本文字集合に追加 | | Yes | | |

lang/cpp11/attributes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ C++11で採用されたもの以外で検討された以下の機能は、属性
171171
- [C++20 `[[nodiscard]]`属性に理由となる文字列を付加できるようにする](/lang/cpp20/nodiscard_should_have_a_reason.md)
172172
- [C++20 `[[nodiscard]]`をコンストラクタのオーバーロードごとに付加できるようにする](/lang/cpp20/nodiscard_for_constructors.md)
173173
- [C++17 `[[fallthrough]]`属性](/lang/cpp17/fallthrough.md)
174+
- [C++17 不明な属性を無視する](/lang/cpp17/non_standard_attributes.md)
175+
- [C++26 属性の無視性を見直し](/lang/cpp26/on_the_ignorability_of_standard_attributes.md)
174176
- [C++20 `[[no_unique_address]]`属性](/lang/cpp20/language_support_for_empty_objects.md)
175177
- [C++20 `[[likely]]`, `[[unlikely]]`属性](/lang/cpp20/likely_and_unlikely_attributes.md)
176178
- [C++20 属性の名前空間を予約](/lang/cpp20/reserving_attribute_namespaces_for_future_use.md)

lang/cpp17/non_standard_attributes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ non_standard_attributes.cpp:7:3: warning: unknown attribute 'unknown_attribute'
5656

5757
## <a id="relative-page" href="#relative-page">関連項目</a>
5858
- [C++11 属性構文](/lang/cpp11/attributes.md)
59+
- [C++26 属性の無視性を見直し](/lang/cpp26/on_the_ignorability_of_standard_attributes.md)
5960

6061
## 参照
61-
- [P0283R2 Standard and non-standard attributes.](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0283r2.html)
62+
- [P0283R2 Standard and non-standard attributes.](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0283r2.html)

lang/cpp26.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ C++26とは、2026年中に改訂される予定の、C++バージョンの通
5959

6060
| 言語機能 | 説明 |
6161
|----------|------|
62-
| [属性の無視性を見直し](/lang/cpp26/on_the_ignorability_of_standard_attributes.md.nolink) | 構文として適格な属性のみを無視できるようにし、そうでない属性の使用を不適格とする |
62+
| [属性の無視性を見直し](/lang/cpp26/on_the_ignorability_of_standard_attributes.md) | 構文として適格な属性のみを無視できるようにし、そうでない属性の使用を不適格とする |
6363
| [構造化束縛への属性を許可](/lang/cpp26/attributes_for_structured_bindings.md) | `auto [a, b [[maybe_unused]], c] = f();`のように構造化束縛の要素に対して属性を付加できるようにする |
6464

6565

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# 属性の無視性を見直し [P2552R3]
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++20時点で、不明な属性を無視する仕様は以下のようになっていた。
14+
15+
「この規格書で規定されていない属性の動作は実装定義である。実装が認識できない属性はすべて無視される。」
16+
17+
この仕様は、規格外の属性を無視するものなのか、規格内の属性も無視してよい意図なのか、という点であいまいだった。
18+
19+
C++26では、以下の方針で属性の無視性を決定する。
20+
21+
1. 構文的な無視性
22+
2. 意味論的な無視性
23+
3. `__has_cpp_attribute`の動作
24+
25+
26+
### 1. 構文的な無視性
27+
標準属性は、構文的に無視してはならず、意味論的に無視するとしても、実装は構文を診断しなければならない。パラメータのあり・なし、およびその形式といった構文が規定されたものであること。
28+
29+
```cpp
30+
// C++26: noreturn属性はパラメータを持たないため構文エラー
31+
[[noreturn("cannot have a reason")]]
32+
int f();
33+
34+
// C++26: deprecated属性のパラメータは文字列でなければならないため構文エラー
35+
[[deprecated(not_a_string)]] int g();
36+
37+
// C++26: nodiscard属性のパラメータは文字列でなければならないため構文エラー
38+
[[nodiscard(this?is!a:balanced%{token[sequence]})]]
39+
int h();
40+
```
41+
42+
### 2. 意味論的な無視性
43+
適格なプログラムが与えられた際、特定の標準属性のインスタンスをすべて削除し、プログラムの観測可能な振る舞いを変化させることが実装に許可される。ただし、削除後の動作が削除前のプログラムにとって適合動作 (conforming behaviour) である場合に限られる。
44+
45+
例として、空のオブジェクトのサイズを削減する[`[[no_unique_address]]`](/lang/cpp20/language_support_for_empty_objects.md)において、`sizeof(Y)`は[`[[no_unique_address]]`](/lang/cpp20/language_support_for_empty_objects.md)属性が無視されなければ`sizeof(int)`と同値となり、無視されれば`sizeof(X) + sizeof(int)`となる。
46+
47+
```cpp
48+
struct X {};
49+
50+
struct Y {
51+
[[no_unique_address]] X x;
52+
int i;
53+
};
54+
55+
int main() {
56+
return sizeof(Y) == sizeof(int);
57+
}
58+
```
59+
60+
`sizeof(Y) == sizeof(int)`の結果が`true``false`どちらであったとしても、適合動作であることに変わりはないため、無視することが許容される。
61+
62+
C++11の策定段階では属性だった[`alignas`](/lang/cpp11/alignas.md) (その後キーワードになった) は、アライメントが変わるとプログラムの適格動作 (mandated behaviour) が変わるため、無視してはならない。
63+
64+
65+
### 3. `__has_cpp_attribute`の動作
66+
標準属性の機能テストマクロは、実装がその属性の (省略可能な) 意味論を実装している場合にのみ正の値を返す。構文チェックのみを行い、意味論を実装しない場合には、正の値を返してはならない。
67+
68+
```cpp
69+
// no_unique_addressの構文チェックと意味論まで実装されている場合に、trueとなる
70+
#if __has_cpp_attribute(no_unique_address)
71+
72+
#endif
73+
```
74+
75+
76+
## <a id="relative-page" href="#relative-page">関連項目</a>
77+
- [C++11 属性構文](/lang/cpp11/attributes.md)
78+
- [C++17 不明な属性を無視する](/lang/cpp17/non_standard_attributes.md)
79+
80+
81+
## 参照
82+
- [P2552R3 On the ignorability of standard attributes](http://open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2552r3.pdf)

0 commit comments

Comments
 (0)