66
77``` cpp
88namespace 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)
0 commit comments