From 788ab800753936d3e2aae211b9fcb5bc6109421d Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Thu, 30 Jun 2016 18:09:29 -0700 Subject: [PATCH] Don't try and instantiate an invalid function in DiscriminatedPtrDetail Summary: THe issue is quite simple: Regardless of the control flow, the previous implementation of this was instantiating `ApplyVisitor1` which declares a return type, but never returns. This refactors `ApplyVisitor1` and `ApplyConstVisitor1` so that the part of the control flow that was previously never reached simply doesn't exist anymore. Reviewed By: yfeldblum Differential Revision: D3479584 fbshipit-source-id: 605a48e39bba6dc14df1af1e76b55ea60f3e69d5 --- folly/detail/DiscriminatedPtrDetail.h | 66 +++++++++++++++------------ 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/folly/detail/DiscriminatedPtrDetail.h b/folly/detail/DiscriminatedPtrDetail.h index 8795321a..c87c9b4b 100644 --- a/folly/detail/DiscriminatedPtrDetail.h +++ b/folly/detail/DiscriminatedPtrDetail.h @@ -110,53 +110,59 @@ struct ConstVisitorResult { typename ConstVisitorResult1::type...>::type type; }; -template struct ApplyVisitor1; +template +struct ApplyVisitor1; -template -struct ApplyVisitor1 { - R operator()(size_t /* index */, V&& /* visitor */, void* /* ptr */) const { - CHECK(false); // NOTREACHED +template +struct ApplyVisitor1<1, V, R, T, Types...> { + R operator()(size_t, V&& visitor, void* ptr) const { + return visitor(static_cast(ptr)); } }; -template -struct ApplyVisitor1 { - R operator()(size_t index, V&& visitor, void* ptr) const { - return (index == 1 ? visitor(static_cast(ptr)) : - ApplyVisitor1()( - index - 1, std::forward(visitor), ptr)); +template +struct ApplyVisitor1 { + R operator()(size_t runtimeIndex, V&& visitor, void* ptr) const { + return runtimeIndex == 1 + ? visitor(static_cast(ptr)) + : ApplyVisitor1()( + runtimeIndex - 1, std::forward(visitor), ptr); } }; -template struct ApplyConstVisitor1; +template +struct ApplyConstVisitor1; -template -struct ApplyConstVisitor1 { - R operator()(size_t /* index */, V&& /* visitor */, void* /* ptr */) const { - CHECK(false); // NOTREACHED +template +struct ApplyConstVisitor1<1, V, R, T, Types...> { + R operator()(size_t, V&& visitor, void* ptr) const { + return visitor(static_cast(ptr)); } }; -template -struct ApplyConstVisitor1 { - R operator()(size_t index, V&& visitor, void* ptr) const { - return (index == 1 ? visitor(static_cast(ptr)) : - ApplyConstVisitor1()( - index - 1, std::forward(visitor), ptr)); +template +struct ApplyConstVisitor1 { + R operator()(size_t runtimeIndex, V&& visitor, void* ptr) const { + return runtimeIndex == 1 + ? visitor(static_cast(ptr)) + : ApplyConstVisitor1()( + runtimeIndex - 1, std::forward(visitor), ptr); } }; template -struct ApplyVisitor - : ApplyVisitor1< - V, typename VisitorResult::type, Types...> { -}; +using ApplyVisitor = ApplyVisitor1< + sizeof...(Types), + V, + typename VisitorResult::type, + Types...>; template -struct ApplyConstVisitor - : ApplyConstVisitor1< - V, typename ConstVisitorResult::type, Types...> { -}; +using ApplyConstVisitor = ApplyConstVisitor1< + sizeof...(Types), + V, + typename ConstVisitorResult::type, + Types...>; } // namespace dptr_detail } // namespace folly -- 2.34.1