cloudflare/ClickHouse
Publicmirrored fromhttps://github.com/cloudflare/ClickHouse
base/ext/make_array_n.h
43lines · modecode
6 years ago
| 1 | #pragma once |
| 2 | |
| 3 | #include <utility> |
| 4 | #include <type_traits> |
| 5 | #include <array> |
| 6 | |
| 7 | |
| 8 | /** \brief Produces std::array of specified size, containing copies of provided object. |
| 9 | * Copy is performed N-1 times, and the last element is being moved. |
| 10 | * This helper allows to initialize std::array in place. |
| 11 | */ |
| 12 | namespace ext |
| 13 | { |
| 14 | namespace detail |
| 15 | { |
| 16 | |
| 17 | template<std::size_t size, typename T, std::size_t... indexes> |
| 18 | constexpr auto make_array_n_impl(T && value, std::index_sequence<indexes...>) |
| 19 | { |
| 20 | /// Comma is used to make N-1 copies of value |
| 21 | return std::array<std::decay_t<T>, size>{ (static_cast<void>(indexes), value)..., std::forward<T>(value) }; |
| 22 | } |
| 23 | |
| 24 | } |
| 25 | |
| 26 | template<typename T> |
| 27 | constexpr auto make_array_n(std::integral_constant<std::size_t, 0>, T &&) |
| 28 | { |
| 29 | return std::array<std::decay_t<T>, 0>{}; |
| 30 | } |
| 31 | |
| 32 | template<std::size_t size, typename T> |
| 33 | constexpr auto make_array_n(std::integral_constant<std::size_t, size>, T && value) |
| 34 | { |
| 35 | return detail::make_array_n_impl<size>(std::forward<T>(value), std::make_index_sequence<size - 1>{}); |
| 36 | } |
| 37 | |
| 38 | template<std::size_t size, typename T> |
| 39 | constexpr auto make_array_n(T && value) |
| 40 | { |
| 41 | return make_array_n(std::integral_constant<std::size_t, size>{}, std::forward<T>(value)); |
| 42 | } |
| 43 | } |