xref: /aosp_15_r20/external/libbrillo/brillo/array_utils.h (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2019 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_ARRAY_UTILS_H_
6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_ARRAY_UTILS_H_
7*1a96fba6SXin Li 
8*1a96fba6SXin Li #include <array>
9*1a96fba6SXin Li #include <utility>
10*1a96fba6SXin Li 
11*1a96fba6SXin Li namespace brillo {
12*1a96fba6SXin Li 
13*1a96fba6SXin Li // Create a std::array from a set of values without manually specifying the
14*1a96fba6SXin Li // size of the array. Note that unlike the make_array likely to make its way
15*1a96fba6SXin Li // into C++20, this function always requires the user to specify ElementType.
16*1a96fba6SXin Li // This is done so that users are not surprised by the element type of resulting
17*1a96fba6SXin Li // arrays when std::common_type is used.
18*1a96fba6SXin Li template <typename ElementType, typename... T>
make_array(T &&...values)19*1a96fba6SXin Li constexpr auto make_array(T&&... values) {
20*1a96fba6SXin Li   return std::array<ElementType, sizeof...(T)>{
21*1a96fba6SXin Li       static_cast<ElementType>(std::forward<T>(values))...};
22*1a96fba6SXin Li }
23*1a96fba6SXin Li 
24*1a96fba6SXin Li }  // namespace brillo
25*1a96fba6SXin Li 
26*1a96fba6SXin Li #endif  // LIBBRILLO_BRILLO_ARRAY_UTILS_H_
27