Skip to content

Commit 676aba0

Browse files
committed
ignore : C++26対応として詳細な定義を追加し、例を追加 (close #1315)
tieページのignoreに関する記述は、とくに消さなくても問題なさそうだったのでそのまま
1 parent 2d81418 commit 676aba0

3 files changed

Lines changed: 71 additions & 3 deletions

File tree

lang/cpp26/nice_placeholder_with_no_name.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ int main() {
8686

8787
## <a id="relative-page" href="#relative-page">関連項目</a>
8888
- [C++17 `[[maybe_unused]]`属性](/lang/cpp17/maybe_unused.md)
89+
- [`std::ignore`](/reference/tuple/ignore.md)
8990

9091
## 参照
9192
- [P2169R4 A nice placeholder with no name](https://open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2169r4.pdf)

reference/tuple/ignore.md

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,77 @@
66

77
```cpp
88
namespace std {
9-
const unspecified ignore; // C++11
10-
inline constexpr unspecified ignore; // C++17
9+
struct ignore-type { // 説明用の定義 (C++26)
10+
constexpr const ignore-type&
11+
operator=(const auto &) const noexcept
12+
{ return *this; }
13+
};
14+
15+
const unspecified ignore; // (1) C++11
16+
inline constexpr unspecified ignore; // (1) C++17
17+
inline constexpr ignore-type ignore; // (1) C++26
1118
}
1219
```
1320
* unspecified[italic]
1421
1522
## 概要
1623
`ignore`は、[`tie()`](tie.md)を使用してタプルから値を抽出する際に、「不要な値」をマーキングするためのプレースホルダーである。
1724
18-
使用例は[`tie()`](tie.md)を参照。
25+
そのほか、関数の戻り値を明示的に無視する際にも使用できる。
26+
27+
C++26以降は、[`<utility>`](/reference/utility.md)をインクルードして使用することもできる。
28+
29+
30+
## 例
31+
### タプルの要素を取り出す際に一部の要素を無視する
32+
```cpp example
33+
#include <iostream>
34+
#include <tuple>
35+
#include <string>
36+
37+
std::tuple<int, char, std::string> f()
38+
{
39+
return {1, 'a', "hello"};
40+
}
41+
42+
int main() {
43+
// char要素は無視する
44+
int a;
45+
std::string c;
46+
std::tie(a, std::ignore, c) = f();
47+
48+
std::cout << a << std::endl;
49+
std::cout << c << std::endl;
50+
}
51+
```
52+
* std::ignore[color ff0000]
53+
54+
#### 出力
55+
```
56+
1
57+
hello
58+
```
59+
60+
### 関数の戻り値を無視する (C++17)
61+
```cpp example
62+
#include <iostream>
63+
#include <tuple>
64+
#include <string>
65+
66+
[[nodiscard]]
67+
int print_string(std::string s)
68+
{
69+
std::cout << s << std::endl;
70+
return 0;
71+
}
72+
73+
int main() {
74+
// 自分の用途ではこの関数は必ず成功するため、
75+
// 戻り値を無視する
76+
std::ignore = print_string("hello");
77+
}
78+
```
79+
* std::ignore[color ff0000]
1980
2081
2182
## バージョン
@@ -33,4 +94,9 @@ namespace std {
3394
- [`std::make_tuple`](make_tuple.md)
3495
- [`std::forward_as_tuple`](forward_as_tuple.md)
3596
- [`std::tie`](tie.md)
97+
- [C++17 `[[nodiscard]]`属性](/lang/cpp17/nodiscard.md)
98+
- [C++26 宣言のみで使用しない変数の名前として`_`をサポート](/lang/cpp26/nice_placeholder_with_no_name.md)
99+
36100
101+
## 参照
102+
- [P2968R2 Make `std::ignore` a first-class object](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2968r2.html)

reference/tuple/tie.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,5 @@ text, b.txt
160160
161161
## 参照
162162
- [LWG2301 Why is std::tie not constexpr?](http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#2301)
163+
- [P2968R2 Make `std::ignore` a first-class object](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2968r2.html)
163164

0 commit comments

Comments
 (0)