std::ignore

From cppreference.com
< cpp‎ | utility‎ | tuple
 
 
Utilities library
General utilities
Date and time
Function objects
Formatting library (C++20)
(C++11)
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)   
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)
Elementary string conversions
(C++17)
(C++17)
 
std::tuple
Member functions
Non-member functions
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20)
Deduction guides(C++17)
Helper classes
ignore
 
Defined in header <tuple>
const /*unspecified*/ ignore;
(since C++11)
(until C++17)
inline constexpr /*unspecified*/ ignore;
(since C++17)

An object of unspecified type such that any value can be assigned to it with no effect. Intended for use with std::tie when unpacking a std::tuple, as a placeholder for the arguments that are not used.

It can also be used to avoid warnings from unused return values of [[nodiscard]] functions.

Possible implementation

namespace detail {
struct ignore_t {
    template <typename T>
    constexpr // required since C++14
    void operator=(T&&) const noexcept {}
};
}
inline constexpr detail::ignore_t ignore; // 'const' only until C++17

Example

  1. Demonstrates the use of std::ignore together with a [[nodiscard]] function.
  2. Unpacks a std::pair<iterator, bool> returned by set.insert(), but only saves the boolean.
#include <iostream>
#include <string>
#include <set>
#include <tuple>
 
[[nodiscard]] int dontIgnoreMe()
{
    return 42;
}
 
int main()
{
    std::ignore = dontIgnoreMe();
 
    std::set<std::string> set_of_str;
    bool inserted = false;
    std::tie(std::ignore, inserted) = set_of_str.insert("Test");
    if (inserted) {
        std::cout << "Value was inserted successfully\n";
    }
}

Output:

Value was inserted successfully

See also

(C++11)
creates a tuple of lvalue references or unpacks a tuple into individual objects
(function template)