Commit eae0e516 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[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: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#63752}
parent 01b5a7ed
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
// Clients of this interface shouldn't depend on lots of asmjs internals. // Clients of this interface shouldn't depend on lots of asmjs internals.
// Do not include anything from src/asmjs here! // Do not include anything from src/asmjs here!
#include <memory>
#include "src/common/globals.h" #include "src/common/globals.h"
namespace v8 { namespace v8 {
......
...@@ -6,32 +6,20 @@ ...@@ -6,32 +6,20 @@
#define V8_BASE_TEMPLATE_UTILS_H_ #define V8_BASE_TEMPLATE_UTILS_H_
#include <array> #include <array>
#include <memory> #include <type_traits>
#include <utility>
namespace v8 { namespace v8 {
namespace base { namespace base {
namespace detail { namespace detail {
// make_array_helper statically iteratively creates the index list 0 .. Size-1. template <typename Function, std::size_t... Indexes>
// A specialization for the base case (first index is 0) finally constructs the constexpr inline auto make_array_helper(Function f,
// array. std::index_sequence<Indexes...>)
// TODO(clemensh): Use std::index_sequence once we have C++14 support. -> std::array<decltype(f(0)), sizeof...(Indexes)> {
template <class Function, std::size_t... Indexes> return {{f(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...> {};
} // namespace detail } // namespace detail
...@@ -42,10 +30,8 @@ struct make_array_helper<Function, FirstIndex, Indexes...> ...@@ -42,10 +30,8 @@ struct make_array_helper<Function, FirstIndex, Indexes...>
// [](std::size_t i) { return static_cast<int>(2 * i); }); // [](std::size_t i) { return static_cast<int>(2 * i); });
// The resulting array will be constexpr if the passed function is constexpr. // The resulting array will be constexpr if the passed function is constexpr.
template <std::size_t Size, class Function> template <std::size_t Size, class Function>
constexpr std::array<typename std::result_of<Function(size_t)>::type, Size> constexpr auto make_array(Function f) {
make_array(Function f) { return detail::make_array_helper(f, std::make_index_sequence<Size>{});
static_assert(Size > 0, "Can only create non-empty arrays");
return detail::make_array_helper<Function, Size - 1>::make_array(f);
} }
// Helper to determine how to pass values: Pass scalars and arrays by value, // Helper to determine how to pass values: Pass scalars and arrays by value,
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <iosfwd> #include <iosfwd>
#include <list> #include <list>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment