std::hash <std::experimental::optional>

Header: <experimental/optional>

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

# Declarations

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

(library fundamentals TS)

# Example

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

# See also