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); }  

live demo


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

c# - How do I get the Nth largest element from a list with duplicates, using LINQ? -

jsp - "Sending a redirect is forbidden after the response has been committed" in sendRedirect -