prvalues from get_ref_default()'s default functor
authorTom Jackson <tjackson@fb.com>
Thu, 8 Sep 2016 09:36:58 +0000 (02:36 -0700)
committerFacebook Github Bot 9 <facebook-github-bot-9-bot@fb.com>
Thu, 8 Sep 2016 09:38:29 +0000 (02:38 -0700)
Summary: This previously allowed `get_ref_default(map, 4, []{ return 6; })`, even though this would form a reference to a temporary, then **use that invalid reference**.

Reviewed By: yfeldblum

Differential Revision: D3802707

fbshipit-source-id: 384d965f69c9d7b6bd3f011c8eff7fe55be7023a

folly/MapUtil.h
folly/test/MapUtilTest.cpp

index 143dadc26314e6f16c50a32d49381dbaa398d763..598567a7682a435b9a9936d1b487553aa1156c30 100644 (file)
@@ -116,7 +116,9 @@ template <
     typename Func,
     typename = typename std::enable_if<std::is_convertible<
         typename std::result_of<Func()>::type,
-        const typename Map::mapped_type&>::value>::type>
+        const typename Map::mapped_type&>::value>::type,
+    typename = typename std::enable_if<
+        std::is_reference<typename std::result_of<Func()>::type>::value>::type>
 const typename Map::mapped_type& get_ref_default(
     const Map& map,
     const typename Map::key_type& key,
index ee1e7b5bd2290b05c69dce7516f210c569656023..7ba12a984d02be21f4dfd7fc2592bac5a1c58f3b 100644 (file)
@@ -85,6 +85,8 @@ TEST(MapUtil, get_ref_default_function) {
       std::addressof(i),
       std::addressof(
           get_ref_default(m, 2, [&i]() -> const int& { return i; })));
+  // statically disallowed:
+  // get_ref_default(m, 2, [] { return 7; });
 }
 
 TEST(MapUtil, get_ptr) {