From: Yedidya Feldblum Date: Thu, 26 Nov 2015 05:48:38 +0000 (-0800) Subject: Fix ExceptionWrapper::with_exception to support lvalue ref functors X-Git-Tag: deprecate-dynamic-initializer~224 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=f0a9aafc8048ea62866c26ef6623834c7824ea4c;p=folly.git Fix ExceptionWrapper::with_exception to support lvalue ref functors Summary: [Folly] Fix `ExceptionWrapper::with_exception` to support lvalue ref functors. Ex: auto handler = [&](const std::runtime_error& e) { handle_runtime_error(e); }; exception_wrapper wrap = get_some_exception_wrapper_from_context(); wrap.with_exception(handler); // broken before this diff, fixed after Reviewed By: markisaa Differential Revision: D2698680 fb-gh-sync-id: 4976ba08e3601e22891d00d79a5dae5118887b71 --- diff --git a/folly/ExceptionWrapper.h b/folly/ExceptionWrapper.h index 70eb996b..f97f1a8e 100644 --- a/folly/ExceptionWrapper.h +++ b/folly/ExceptionWrapper.h @@ -330,7 +330,9 @@ private: struct impl { using arg_type = A; }; template struct impl { using arg_type = A; }; - using arg_type = typename impl::arg_type; + using functor_decayed = typename std::decay::type; + using functor_op = decltype(&functor_decayed::operator()); + using arg_type = typename impl::arg_type; using arg_type_decayed = typename std::decay::type; }; diff --git a/folly/test/ExceptionWrapperTest.cpp b/folly/test/ExceptionWrapperTest.cpp index 61395df7..265c5b02 100644 --- a/folly/test/ExceptionWrapperTest.cpp +++ b/folly/test/ExceptionWrapperTest.cpp @@ -210,14 +210,14 @@ TEST(ExceptionWrapper, with_exception_deduction) { EXPECT_FALSE(ew.with_exception([](std::logic_error&) {})); } -TEST(ExceptionWrapper, with_exception_deduction_const) { +TEST(ExceptionWrapper, with_exception_deduction_exn_const) { auto ew = make_exception_wrapper("hi"); EXPECT_TRUE(ew.with_exception([](const std::runtime_error&) {})); EXPECT_TRUE(ew.with_exception([](const std::exception&) {})); EXPECT_FALSE(ew.with_exception([](const std::logic_error&) {})); } -TEST(ExceptionWrapper, with_exception_deduction_const_const) { +TEST(ExceptionWrapper, with_exception_deduction_wrap_const_exn_const) { const auto cew = make_exception_wrapper("hi"); EXPECT_TRUE(cew.with_exception([](const std::runtime_error&) {})); EXPECT_TRUE(cew.with_exception([](const std::exception&) {})); @@ -231,6 +231,18 @@ TEST(ExceptionWrapper, with_exception_deduction_returning) { EXPECT_FALSE(ew.with_exception([](std::logic_error&) { return nullptr; })); } +namespace { +template +T& r_to_l(T v) { return std::ref(v); } +} + +TEST(ExceptionWrapper, with_exception_deduction_functor_lvalue) { + auto ew = make_exception_wrapper("hi"); + EXPECT_TRUE(ew.with_exception(r_to_l([](std::runtime_error&) {}))); + EXPECT_TRUE(ew.with_exception(r_to_l([](std::exception&) {}))); + EXPECT_FALSE(ew.with_exception(r_to_l([](std::logic_error&) {}))); +} + TEST(ExceptionWrapper, non_std_exception_test) { int expected = 17;