From: Tom Jackson Date: Thu, 8 Sep 2016 09:36:58 +0000 (-0700) Subject: prvalues from get_ref_default()'s default functor X-Git-Tag: v2016.09.12.00~12 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=cf784212df2255844336b7ff0c7d36ee9037fa88;p=folly.git prvalues from get_ref_default()'s default functor 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 --- diff --git a/folly/MapUtil.h b/folly/MapUtil.h index 143dadc2..598567a7 100644 --- a/folly/MapUtil.h +++ b/folly/MapUtil.h @@ -116,7 +116,9 @@ template < typename Func, typename = typename std::enable_if::type, - const typename Map::mapped_type&>::value>::type> + const typename Map::mapped_type&>::value>::type, + typename = typename std::enable_if< + std::is_reference::type>::value>::type> const typename Map::mapped_type& get_ref_default( const Map& map, const typename Map::key_type& key, diff --git a/folly/test/MapUtilTest.cpp b/folly/test/MapUtilTest.cpp index ee1e7b5b..7ba12a98 100644 --- a/folly/test/MapUtilTest.cpp +++ b/folly/test/MapUtilTest.cpp @@ -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) {