From e651ab6ff602f064a1beb84d58d042bc491008e1 Mon Sep 17 00:00:00 2001 From: Clemens Hammacher Date: Fri, 13 Sep 2019 15:10:24 +0200 Subject: [PATCH] [base] Reimplement {base::fold} in C++14 Just two functions instead of partially specialized structs. Also, no need to compute the return type(s), just use {auto}. R=tebbi@chromium.org Bug: v8:9396 Change-Id: I840af52c3caac622aded8bd7656a5437abb2c8ef Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1801845 Commit-Queue: Clemens Hammacher Reviewed-by: Tobias Tebbi Cr-Commit-Position: refs/heads/master@{#63756} --- src/base/template-utils.h | 39 +++++++++------------------------------ 1 file changed, 9 insertions(+), 30 deletions(-) diff --git a/src/base/template-utils.h b/src/base/template-utils.h index 0d62e5110b..146f8d6e6a 100644 --- a/src/base/template-utils.h +++ b/src/base/template-utils.h @@ -58,38 +58,17 @@ struct has_output_operator() << std::declval()))> : std::true_type {}; -namespace detail { - -template -struct fold_helper { - static_assert(sizeof...(Ts) == 0, "this is the base case"); - using result_t = typename std::remove_reference::type; - static constexpr T&& fold(Func func, T&& first) { - return std::forward(first); - } -}; +// Fold all arguments from left to right with a given function. +template +constexpr auto fold(Func func, T&& t) { + return std::forward(t); +} template -struct fold_helper { - using folded_t = typename std::result_of::type; - using next_fold_helper = fold_helper; - using result_t = typename next_fold_helper::result_t; - static constexpr result_t fold(Func func, T1&& first, T2&& second, - Ts&&... more) { - return next_fold_helper::fold( - func, func(std::forward(first), std::forward(second)), - std::forward(more)...); - } -}; - -} // namespace detail - -// Fold all arguments from left to right with a given function. -template -constexpr auto fold(Func func, Ts&&... more) -> - typename detail::fold_helper::result_t { - return detail::fold_helper::fold(func, - std::forward(more)...); +constexpr auto fold(Func func, T1&& first, T2&& second, Ts&&... more) { + auto&& folded = func(std::forward(first), std::forward(second)); + return fold(std::move(func), std::forward(folded), + std::forward(more)...); } // {is_same::value} is true if all Ts are the same, false otherwise.