Home > Article > Backend Development > How to Elegantly Initialize a `std::array` with Non-Default-Constructible Types?
Elegant Initialization of std::array with Non-Default-Constructible Type
Initializing a std::array with a non-default-constructible element type can be a cumbersome task. Manually repeating the value n times is inefficient and error-prone for large n.
To address this issue, a more elegant approach involves utilizing a sequence-type and a generator. The key idea is to create a sequence of indices from 0 to n-1 and then use a function to repeatedly apply a value to each index.
Here's an implementation:
<code class="cpp">template<typename T, int...N> auto repeat(T value, seq<N...>) -> std::array<T, sizeof...(N)> { // Unpack N, repeating `value` sizeof...(N) times // Note that (X, value) evaluates to value return {(N, value)...}; }</code>
To initialize an std::array using this approach:
<code class="cpp">template<typename T, int N> void f(T value) { // genseq_t<N> is seq<0,1,...N-1> std::array<T, N> items = repeat(value, genseq_t<N>{}); }</code>
Additionally, the following definitions are used:
<code class="cpp">template<int ... N> struct seq { using type = seq<N...>; static const std::size_t size = sizeof ... (N); template<int I> struct push_back : seq<N..., I> {}; }; template<int N> struct genseq : genseq<N-1>::type::template push_back<N-1> {}; template<> struct genseq<0> : seq<> {}; template<int N> using genseq_t = typename genseq<N>::type;</code>
This solution provides an efficient and elegant way to initialize std::array with non-default-constructible types, regardless of the value of n.
The above is the detailed content of How to Elegantly Initialize a `std::array` with Non-Default-Constructible Types?. For more information, please follow other related articles on the PHP Chinese website!