Skip to content

Commit 38888cf

Browse files
committed
C++23 : 「添字演算子の多次元サポート」を追加 (close #1028)
1 parent 1203319 commit 38888cf

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

implementation-status.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@
257257
| P2324R2: [複合文の末尾へのラベルを許可](/lang/cpp23/labels_at_the_end_of_compound_statements.md) | C互換のため、複合文の末尾でのgoto文のラベルを許可する | 13 | 16 | 2023.2 | - |
258258
| P0847R7: [自身のオブジェクトを明示的にパラメータとして指定する](/lang/cpp23/deducing_this.md.nolink) | メンバ関数が`*this`の型・オブジェクトをパラメータとしてとり、`*this`オブジェクトがconst/非const、左辺値/右辺値であるかをメンバ関数内で識別できるようにする | - | 18 | - | 2022 Update 2 (partial) |
259259
| P1847R4: [アクセス制御の異なるメンバ変数のレイアウトを宣言順に規定](/lang/cpp23/make_declaration_order_layout_mandated.md) | アクセス制御の異なるメンバ変数のレイアウトは並び替えを許可されていたが宣言順に規定する | Yes | Yes | - | 2022 |
260-
| P2128R6: [添字演算子の多次元サポート](/lang/cpp23/multidimensional_subscript_operator.nd.nolink) | `operator[](int x, int y, int z)`のように添字演算子のオーバーロードで複数のパラメータをとることを許可 | 12 | 15 | 2022.2 | - |
260+
| P2128R6: [添字演算子の多次元サポート](/lang/cpp23/multidimensional_subscript_operator.md) | `operator[](int x, int y, int z)`のように添字演算子のオーバーロードで複数のパラメータをとることを許可 | 12 | 15 | 2022.2 | - |
261261
| P1169R4: [`this`ポインタをもつ必要のない演算子を`static`として宣言できるようにする](/lang/cpp23/static_operator.md) | 状態をもたないいくつかの演算子を`static`として宣言できるようにする | 13 | 16 | 2023.2 | - |
262262
| P2201R1: [異なる文字エンコーディングをもつ文字列リテラルの連結を不適格とする](/lang/cpp23/mixed_string_literal_concatenation.md) | `auto a = u8"" L"";`のような異なる文字エンコーディング同士での文字列リテラルを連結を禁止する | Yes | Yes | 2022.2 | Yes |
263263
| P2029R4: [文字・文字列リテラル中の数値・ユニバーサルキャラクタのエスケープに関する問題解決](/lang/cpp23/numeric_and_universal_character_escapes_in_character_and_string_literals.md.nolink) | | - | - | - | - |

lang/cpp20/deprecate_uses_of_the_comma_operator_in_subscripting_expressions.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,8 @@ ar[x, y] // C++17:OK, C++20:非推奨
1515
既存のカンマ演算子をオーバーロードしたコードを使用する場合は、丸カッコで囲むこと。
1616

1717

18+
## 関連項目
19+
- [C++23 添字演算子の多次元サポート](/lang/cpp23/multidimensional_subscript_operator.md)
20+
1821
## 参照
1922
- [P1161R3 Deprecate uses of the comma operator in subscripting expressions](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1161r3.html)

lang/cpp23.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ C++23とは、2023年中に改訂される予定の、C++バージョンの通
4040
|----------|------|
4141
| [自身のオブジェクトを明示的にパラメータとして指定する](cpp23/deducing_this.md.nolink) | メンバ関数が`*this`の型・オブジェクトをパラメータとしてとり、`*this`オブジェクトがconst/非const、左辺値/右辺値であるかをメンバ関数内で識別できるようにする |
4242
| [アクセス制御の異なるメンバ変数のレイアウトを宣言順に規定](cpp23/make_declaration_order_layout_mandated.md) | アクセス制御の異なるメンバ変数のレイアウトは並び替えを許可されていたが宣言順に規定する |
43-
| [添字演算子の多次元サポート](cpp23/multidimensional_subscript_operator.nd.nolink) | `operator[](int x, int y, int z)`のように添字演算子のオーバーロードで複数のパラメータをとることを許可 |
43+
| [添字演算子の多次元サポート](cpp23/multidimensional_subscript_operator.md) | `operator[](int x, int y, int z)`のように添字演算子のオーバーロードで複数のパラメータをとることを許可 |
4444
| [`this`ポインタをもつ必要のない演算子を`static`として宣言できるようにする](cpp23/static_operator.md) | 状態をもたないいくつかの演算子を`static`として宣言できるようにする |
4545

4646

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# 添字演算子の多次元サポート
2+
* cpp23[meta cpp]
3+
4+
## 概要
5+
C++23では、多次元配列クラスを定義するために必要となる、添字演算子のオーバーロードを定義する際の複数引数を許可する。
6+
7+
```cpp
8+
R operator[](int x, int y, int z);
9+
```
10+
11+
複数の許可がされるまでは、以下のような回避策がとられていた:
12+
13+
- 添字アクセスを、関数呼び出し演算子で代用
14+
- カンマ演算子をオーバーロードして、複数引数をひとつの引数にまとめる
15+
16+
17+
##
18+
```cpp example
19+
struct Matrix2x2 {
20+
float data[2 * 2];
21+
22+
float& operator[](int x, int y) {
23+
return data[y * 2 + x];
24+
}
25+
};
26+
27+
#include <iostream>
28+
int main() {
29+
Matrix2x2 mat = {
30+
1.0f, 2.0f,
31+
3.0f, 4.0f
32+
};
33+
34+
float r = mat[0, 1];
35+
std::cout << r << std::endl;
36+
}
37+
```
38+
39+
### 出力
40+
```
41+
3
42+
```
43+
44+
## 関連項目
45+
- [C++20 添字演算子内でのカンマ演算子の使用を非推奨化](/lang/cpp20/deprecate_uses_of_the_comma_operator_in_subscripting_expressions.md)
46+
- [`std::mdspan`](/reference/mdspan/mdspan.md)
47+
48+
49+
## 参照
50+
- [P2128R6 Multidimensional subscript operator](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2128r6.pdf)

reference/mdspan/mdspan.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ int main()
181181
- [`extents`](extents.md)
182182
- [LayoutMappingPolicy](LayoutMappingPolicy.md)
183183
- [AccessorPolicy](AccessorPolicy.md)
184+
- [C++23 添字演算子の多次元サポート](/lang/cpp23/multidimensional_subscript_operator.md)
184185

185186

186187
## 参照

0 commit comments

Comments
 (0)