From 0f122a828426d5347e8b0ce78967e8c81001b906 Mon Sep 17 00:00:00 2001 From: Giuseppe Ottaviano Date: Mon, 20 Nov 2017 23:10:34 -0800 Subject: [PATCH] Revert D6366352: [folly] Split get_default() into two for deferred default construction Summary: This reverts commit db55b944ca63e565997094c11b90c4ebe98531ce bypass-lint Differential Revision: D6366352 fbshipit-source-id: e25906409186b077ef9117aa524cc7c86314ae12 --- folly/MapUtil.h | 27 ++++++------------------- folly/test/MapUtilTest.cpp | 40 -------------------------------------- 2 files changed, 6 insertions(+), 61 deletions(-) diff --git a/folly/MapUtil.h b/folly/MapUtil.h index b95da417..b1fbfadc 100644 --- a/folly/MapUtil.h +++ b/folly/MapUtil.h @@ -18,7 +18,6 @@ #include #include -#include #include namespace folly { @@ -27,27 +26,13 @@ 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) { - auto pos = map.find(key); - 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) { +template +typename Map::mapped_type get_default( + const Map& map, + const Key& key, + const typename Map::mapped_type& dflt = typename Map::mapped_type()) { auto pos = map.find(key); - if (pos != map.end()) { - return pos->second; - } else { - // if elision from function parameters was allowed, then we could make the - // third parameter a value parameter and just elide that into the return - // value, but sadly that is not allowed (yet) - return std::forward(dflt); - } + return (pos != map.end() ? pos->second : dflt); } /** diff --git a/folly/test/MapUtilTest.cpp b/folly/test/MapUtilTest.cpp index 693f7235..52e63e81 100644 --- a/folly/test/MapUtilTest.cpp +++ b/folly/test/MapUtilTest.cpp @@ -16,7 +16,6 @@ #include -#include #include #include @@ -239,7 +238,6 @@ struct GetRefDefaultPathCompiles< std::declval(), std::declval(), std::declval()))>> : std::true_type {}; - } // namespace TEST(MapUtil, get_ref_default_path_temporary) { @@ -248,41 +246,3 @@ TEST(MapUtil, get_ref_default_path_temporary) { EXPECT_FALSE(GetRefDefaultPathCompiles::value); EXPECT_FALSE(GetRefDefaultPathCompiles::value); } - -namespace { - -class TestConstruction { - public: - static std::size_t numberDefaultConstructs; - TestConstruction() { - ++numberDefaultConstructs; - } - TestConstruction(TestConstruction&&) = default; - TestConstruction(const TestConstruction&) = default; - - TestConstruction& operator=(const TestConstruction&) = delete; - TestConstruction& operator=(TestConstruction&&) = delete; -}; - -std::size_t TestConstruction::numberDefaultConstructs = 0; - -} // namespace - -TEST(MapUtil, test_get_default_deferred_construction) { - auto map = std::unordered_map{}; - map.insert({1, TestConstruction{}}); - - EXPECT_EQ(TestConstruction::numberDefaultConstructs, 1); - - { - auto val = get_default(map, 1); - EXPECT_EQ(TestConstruction::numberDefaultConstructs, 1); - static_cast(val); - } - - { - auto val = get_default(map, 1, TestConstruction{}); - EXPECT_EQ(TestConstruction::numberDefaultConstructs, 2); - static_cast(val); - } -} -- 2.34.1