#include <folly/Conv.h>
#include <folly/Optional.h>
+#include <folly/functional/Invoke.h>
#include <tuple>
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 <class Map, typename Key = typename Map::key_type>
-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, typename Key>
+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<!is_invocable<Value>::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<Value>(dflt));
}
/**
#include <folly/MapUtil.h>
+#include <cstddef>
#include <map>
#include <unordered_map>
EXPECT_FALSE(GetRefDefaultPathCompiles<const int&&>::value);
EXPECT_FALSE(GetRefDefaultPathCompiles<int&&>::value);
}
+
+namespace {
+
+class TestConstruction {
+ public:
+ TestConstruction() {
+ EXPECT_TRUE(false);
+ }
+ TestConstruction(TestConstruction&&) {
+ EXPECT_TRUE(false);
+ }
+ TestConstruction(const TestConstruction&) {
+ EXPECT_TRUE(false);
+ }
+
+ explicit TestConstruction(std::string&& string)
+ : string_{std::move(string)} {}
+ explicit TestConstruction(int&& integer) : integer_{integer} {}
+
+ TestConstruction& operator=(const TestConstruction&) = delete;
+ TestConstruction& operator=(TestConstruction&&) = delete;
+
+ int integer_{};
+ std::string string_{};
+};
+
+} // namespace
+
+TEST(MapUtil, test_get_default_deferred_construction) {
+ auto map = std::unordered_map<int, TestConstruction>{};
+ map.emplace(
+ std::piecewise_construct,
+ std::forward_as_tuple(1),
+ std::forward_as_tuple(1));
+
+ EXPECT_EQ(map.at(1).integer_, 1);
+
+ {
+ auto val = get_default(map, 0, 1);
+ EXPECT_EQ(val.integer_, 1);
+ EXPECT_EQ(val.string_, "");
+ }
+
+ {
+ auto val = get_default(map, 0, "something");
+ EXPECT_EQ(val.integer_, 0);
+ EXPECT_EQ(val.string_, "something");
+ }
+}