[base] Reimplement {make_array} using C++14

This reimplements the {base::make_array} helper using
{std::index_sequence}. This avoids the need to recursively create index
lists in template argument packs, and replaces a partially specialized
struct by a single function.

R=tebbi@chromium.org

Bug: v8:9396
Change-Id: I60369bfac6cb9abc889ed658208411949ca9ec07
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1800575
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#63752}
This commit is contained in:
Clemens Hammacher 2019-09-13 13:11:42 +02:00 committed by Commit Bot
parent 01b5a7ed67
commit eae0e5160f
3 changed files with 13 additions and 24 deletions

View File

@ -7,6 +7,8 @@
// Clients of this interface shouldn't depend on lots of asmjs internals.
// Do not include anything from src/asmjs here!
#include <memory>
#include "src/common/globals.h"
namespace v8 {

View File

@ -6,32 +6,20 @@
#define V8_BASE_TEMPLATE_UTILS_H_
#include <array>
#include <memory>
#include <type_traits>
#include <utility>
namespace v8 {
namespace base {
namespace detail {
// make_array_helper statically iteratively creates the index list 0 .. Size-1.
// A specialization for the base case (first index is 0) finally constructs the
// array.
// TODO(clemensh): Use std::index_sequence once we have C++14 support.
template <class Function, std::size_t... Indexes>
struct make_array_helper;
template <class Function, std::size_t... Indexes>
struct make_array_helper<Function, 0, Indexes...> {
constexpr static std::array<typename std::result_of<Function(size_t)>::type,
sizeof...(Indexes) + 1>
make_array(Function f) {
return {{f(0), f(Indexes)...}};
}
};
template <class Function, std::size_t FirstIndex, std::size_t... Indexes>
struct make_array_helper<Function, FirstIndex, Indexes...>
: make_array_helper<Function, FirstIndex - 1, FirstIndex, Indexes...> {};
template <typename Function, std::size_t... Indexes>
constexpr inline auto make_array_helper(Function f,
std::index_sequence<Indexes...>)
-> std::array<decltype(f(0)), sizeof...(Indexes)> {
return {{f(Indexes)...}};
}
} // namespace detail
@ -42,10 +30,8 @@ struct make_array_helper<Function, FirstIndex, Indexes...>
// [](std::size_t i) { return static_cast<int>(2 * i); });
// The resulting array will be constexpr if the passed function is constexpr.
template <std::size_t Size, class Function>
constexpr std::array<typename std::result_of<Function(size_t)>::type, Size>
make_array(Function f) {
static_assert(Size > 0, "Can only create non-empty arrays");
return detail::make_array_helper<Function, Size - 1>::make_array(f);
constexpr auto make_array(Function f) {
return detail::make_array_helper(f, std::make_index_sequence<Size>{});
}
// Helper to determine how to pass values: Pass scalars and arrays by value,

View File

@ -7,6 +7,7 @@
#include <iosfwd>
#include <list>
#include <memory>
#include <string>
#include <vector>