Skip to content

Instantly share code, notes, and snippets.

@airglow923
Created October 9, 2020 07:39
Show Gist options
  • Save airglow923/3dbae1031ec0fd0cc176e69e5aea35d6 to your computer and use it in GitHub Desktop.
Save airglow923/3dbae1031ec0fd0cc176e69e5aea35d6 to your computer and use it in GitHub Desktop.
Map wrapper using class template argument deduction (CTAD).
#include <map>
#include <unordered_map>
template<
template<typename, typename> typename Map,
typename K,
typename V
>
class AnyMap {
public:
template<typename M>
AnyMap(const M& m) {}
template<
template<typename, typename> typename M,
typename A,
typename B
>
AnyMap(const M<A, B>& m) {}
private:
Map<K, V> data;
};
template<
template<typename, typename> typename M,
typename K,
typename V
>
AnyMap(const M<K, V>&) -> AnyMap<M, K, V>;
int main()
{
std::map<int, char*> map;
std::unordered_map<char, double> um;
AnyMap m1{map};
AnyMap m2{um};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment