Namespaces
Variants

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

From cppreference.net
ヘッダーで定義 <experimental/optional>
template < class T >
struct hash < std:: experimental :: optional < T >> ;
(ライブラリ基盤 TS)

std::hash std::experimental::optional クラスに対するテンプレート特殊化は、ユーザーが optional オブジェクトに含まれる値のハッシュ値を取得することを可能にします。

テンプレートパラメータ

T - optionalオブジェクトに含まれる値の型。特殊化 std:: hash < T > はクラステンプレート hash の要件を満たさなければならない。

#include <experimental/optional>
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std::literals;
int main()
{
    // hash<optional>により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';
}

出力例:

def abc (null)

関連項目

(C++11)
ハッシュ関数オブジェクト
(クラステンプレート)