|
|
Member functions | ||||
Non-member functions | ||||
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20) | ||||
Helper concepts | ||||
(C++23) | ||||
Helper classes | ||||
(C++23) | ||||
(C++23) | ||||
ignore | ||||
Deduction guides(C++17) |
Defined in header <tuple> | ||
Defined in header <utility> | ||
(1) | ||
const/*ignore-type*/ ignore; | (since C++11) (until C++14) | |
constexpr/*ignore-type*/ ignore; | (since C++14) (inline since c++17) | |
(2) | ||
struct/*ignore-type*/ { | (since C++11) (until C++14) (exposition only*) | |
struct/*ignore-type*/ { | (since C++14) (exposition only*) | |
std::ignore
.Contents |
Avoid expression or a volatile bit-field value cannot be assigned tostd::ignore
.
std::ignore
is intended for use withstd::tie when unpacking astd::tuple, as a placeholder for the arguments that are not used, but can be used for any unwanted assignment.
Some code guides recommend usingstd::ignore
to avoid warnings from unused return values of[[nodiscard]]
functions, even though an assignment isn't required.
For ignoring values not requiring assignment, one may cast tovoid. For variables that have names, but whose value is unused, one may cast those tovoid or declare those variables with[[maybe_unused]]
.
std::ignore
together with a[[nodiscard]]
function.#include <iostream>#include <set>#include <string>#include <tuple> [[nodiscard]]int dontIgnoreMe(){return42;} int main(){ std::ignore= dontIgnoreMe(); std::set<std::string> set_of_str;if(bool inserted{false};std::tie(std::ignore, inserted)= set_of_str.insert("Test"), inserted)std::cout<<"Value was inserted successfully.\n";}
Output:
Value was inserted successfully.
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2773 | C++14 | std::tuple was madeconstexpr butstd::ignore was not yet | madeconstexpr |
P2968R2 | C++11 | the behavior ofstd::ignore outside ofstd::tie was not formally specified | made fully specified |
(C++11) | creates atuple of lvalue references or unpacks a tuple into individual objects (function template)[edit] |