1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef __TO_ARRAY_H
17 #define __TO_ARRAY_H
18
19 // supported since c++20 see https://en.cppreference.com/w/cpp/container/array/to_array
20 #if (defined(__cpp_lib_to_array) && __cpp_lib_to_array < 201907L) || __cplusplus < 202002L
21 #include <cstdint>
22 #include <array>
23 #include <type_traits>
24
25 namespace detail {
26 template<class T, std::size_t N, std::size_t... I>
to_array_impl(T (& a)[N],std::index_sequence<I...>)27 constexpr std::array<std::remove_cv_t<T>, N> to_array_impl(T (&a)[N], std::index_sequence<I...>)
28 {
29 return { { a[I]... } };
30 }
31 } // namespace detail
32
33 template<class T, std::size_t N>
to_array(T (& a)[N])34 constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N])
35 {
36 return detail::to_array_impl(a, std::make_index_sequence<N>{});
37 }
38 #endif
39 #endif