X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FMapUtil.h;h=433489273785a6f2677c6f36563ea2a292d8fb58;hp=b1fbfadcaf59ecf3db6d14e80f35e472152fa249;hb=5a07e203d79324b68d69f294fa38e43b9671e9b1;hpb=d7629090e2c23da58ffc8977b22ccb17a25ae51c diff --git a/folly/MapUtil.h b/folly/MapUtil.h index b1fbfadc..43348927 100644 --- a/folly/MapUtil.h +++ b/folly/MapUtil.h @@ -18,6 +18,7 @@ #include #include +#include #include namespace folly { @@ -26,13 +27,21 @@ namespace folly { * Given a map and a key, return the value corresponding to the key in the map, * or a given default value if the key doesn't exist in the map. */ -template -typename Map::mapped_type get_default( - const Map& map, - const Key& key, - const typename Map::mapped_type& dflt = typename Map::mapped_type()) { +template +typename Map::mapped_type get_default(const Map& map, const Key& key) { auto pos = map.find(key); - return (pos != map.end() ? pos->second : dflt); + return (pos != map.end()) ? (pos->second) : (typename Map::mapped_type{}); +} +template < + class Map, + typename Key = typename Map::key_type, + typename Value = typename Map::mapped_type, + typename std::enable_if::value>::type* = nullptr> +typename Map::mapped_type +get_default(const Map& map, const Key& key, Value&& dflt) { + using M = typename Map::mapped_type; + auto pos = map.find(key); + return (pos != map.end()) ? (pos->second) : M(std::forward(dflt)); } /**