How do I use heterogeneous lookup with std::unordered_set?

In C++, heterogeneous lookups allow you to search for different types in a data structure without needing to convert them to a common type first. The `std::unordered_set` is a part of the C++ Standard Library and can be used to implement this feature effectively. You can store elements of different types in an `std::unordered_set` by using `std::any`, `std::variant`, or a custom type that can handle various types efficiently.

Here's an example demonstrating how to utilize heterogeneous lookup with `std::unordered_set` using `std::variant`.

#include <iostream> #include <unordered_set> #include <variant> using VariantType = std::variant; struct VariantHasher { std::size_t operator()(const VariantType& v) const { return std::visit([](auto&& arg) { return std::hash<:decay_t>>{}(arg); }, v); } }; struct VariantEqual { bool operator()(const VariantType& lhs, const VariantType& rhs) const { return lhs == rhs; } }; int main() { std::unordered_set mySet; mySet.insert(10); mySet.insert(std::string("Hello")); // Heterogeneous lookups for (const auto& value : mySet) { std::visit([](auto&& arg) { std::cout << arg << std::endl; }, value); } return 0; }

heterogeneous lookup std::unordered_set C++ C++ Standard Library std::variant std::any