- tuple[meta header]
- std[meta namespace]
- variable[meta id-type]
- cpp11[meta cpp]
namespace std {
struct ignore-type { // 説明用の定義 (C++26)
constexpr const ignore-type&
operator=(const auto &) const noexcept
{ return *this; }
};
const unspecified ignore; // (1) C++11
inline constexpr unspecified ignore; // (1) C++17
inline constexpr ignore-type ignore; // (1) C++26
}
- unspecified[italic]
ignore
は、tie()
を使用してタプルから値を抽出する際に、「不要な値」をマーキングするためのプレースホルダーである。
そのほか、関数の戻り値を明示的に無視する際にも使用できる。
C++26以降は、<utility>
をインクルードして使用することもできる。
#include <iostream>
#include <tuple>
#include <string>
std::tuple<int, char, std::string> f()
{
return {1, 'a', "hello"};
}
int main() {
// char要素は無視する
int a;
std::string c;
std::tie(a, std::ignore, c) = f();
std::cout << a << std::endl;
std::cout << c << std::endl;
}
- std::ignore[color ff0000]
1
hello
#include <iostream>
#include <tuple>
#include <string>
[[nodiscard]]
int print_string(std::string s)
{
std::cout << s << std::endl;
return 0;
}
int main() {
// 自分の用途ではこの関数は必ず成功するため、
// 戻り値を無視する
std::ignore = print_string("hello");
}
- std::ignore[color ff0000]
- C++11
- Clang: ?
- GCC: 4.6.1 [mark verified]
- ICC:
- Visual C++: 2008 [mark verified], 2010 [mark verified]