c++ - How can I make a function that takes either a map or an unordered_map? -
i'm implementing template-based serialization. implemented templated function std::map, i'm using std::unordered_map. rather not copy & paste entire function , change parameter type. there way make template takes map or unordered map?
template <typename map> void generic_foo(map& map) { // generic implementation of function // works unordered_map , map using k = typename map::key_type; using t = typename map::mapped_type; } // matches possible implementation of std::unorderd_map template <class key, class t, class hash, class pred, class alloc> void foo(std::unordered_map<key, t, hash, pred, alloc>& m) { // signature matched! forward implementation generic_foo(m); } // matches possible implementation of std::map template <class key, class t, class compare, class alloc> void foo(std::map<key, t, compare, alloc>& m) { // signature matched! forward implementation generic_foo(m); }
Comments
Post a Comment