std::hash<std::optional>

Header: <optional>

The template specialization of std::hash for the std::optional class allows users to obtain hashes of the values contained in optional objects.

# Declarations

template< class T >
struct hash<std::optional<T>>;

(since C++17)

# Example

#include <iostream>
#include <optional>
#include <string>
#include <unordered_set>
 
using namespace std::literals;
 
int main()
{
    using OptStr = std::optional<std::string>;
 
    // hash<optional> makes it possible to use unordered_set
    std::unordered_set<OptStr> s =
    {
        "ABC"s, "abc"s, std::nullopt, "def"s
    };
 
    for (const auto& o : s)
        std::cout << o.value_or("(null)") << '\t' << std::hash<OptStr>{}(o) << '\n';
}

# See also