#include <variant>
#include <unordered_map>
struct Foo {int a, b;};
struct Bar {};
struct Baz {};
auto main() -> int {
const std::unordered_map<std::variant<Foo, Bar, Baz>, int> hello = {
{Foo{1, 2}, 1}
};
return 0;
}
Clang complains of:
error: static_assert failed due to requirement '__is_invocable<const std::hash<std::variant<Foo, Bar, Baz> > &, const std::variant<Foo, Bar, Baz>&>{}' "hash function must be invocable with an argument of key type"`
and numerous other errors.
We can already use variants in unordered_map
like so:
const std::unordered_map<std::variant<std::string, int>, int> a = {
{"This is a test", 1},
{200, 2}
};
Is there a way to use constructors of bare-bone structs as keys in this way?