简体   繁体   中英

How to initialize std::array<T, n> elegantly if T is not default constructible?

How do I initialize std::array<T, n> if T is not default constructible ?

I know it's possible to initialize it like that:

T t{args};
std::array<T, 5> a{t, t, t, t, t};

But n for me is template parameter:

template<typename T, int N>
void f(T value)
{
    std::array<T, N> items = ??? 
}

And even if it wasn't template, it's quite ugly to repeat value by hand if n is too large.

Given N, you could generate a sequence-type called seq<0,1,2,3,...N-1> using a generator called genseq_t<> , then do this:

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>{});
}

where repeat is defined as:

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)...}; 
}

And the rest is defined as:

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;

Online demo

Hope that helps.

Sadly the existing answers here don't work for non-copyable types. So I took @Nawaz answer and modified it:

#include <utility>
#include <array>


template<typename T, size_t...Ix, typename... Args>
std::array<T, sizeof...(Ix)> repeat(std::index_sequence<Ix...>, Args &&... args) {
   return {{((void)Ix, T(args...))...}};
}

template<typename T, size_t N>
class initialized_array: public std::array<T, N> {
public:
    template<typename... Args>
    initialized_array(Args &&... args)
        : std::array<T, N>(repeat<T>(std::make_index_sequence<N>(), std::forward<Args>(args)...)) {}
};

Note that this is an std::array subclass so that one can easily write

class A { 
    A(int, char) {}
}

...

class C {
    initialized_array<A, 5> data;

    ...

    C(): data(1, 'a') {}
}

Without repeating the type and size. Of course, this way can also be implemented as a function initialize_array .

Following will solve your issue:

#if 1 // Not in C++11, but in C++1y (with a non linear better version)

template <std::size_t ...> struct index_sequence {};

template <std::size_t I, std::size_t ...Is>
struct make_index_sequence : make_index_sequence<I - 1, I - 1, Is...> {};

template <std::size_t ... Is>
struct make_index_sequence<0, Is...> : index_sequence<Is...> {};

#endif

namespace detail
{
    template <typename T, std::size_t ... Is>
    constexpr std::array<T, sizeof...(Is)>
    create_array(T value, index_sequence<Is...>)
    {
        // cast Is to void to remove the warning: unused value
        return {{(static_cast<void>(Is), value)...}};
    }
}

template <std::size_t N, typename T>
constexpr std::array<T, N> create_array(const T& value)
{
    return detail::create_array(value, make_index_sequence<N>());
}

So test it:

struct NoDefaultConstructible {
    constexpr NoDefaultConstructible(int i) : m_i(i) { }
    int m_i;
};

int main()
{
    constexpr auto ar1 = create_array<10>(NoDefaultConstructible(42));
    constexpr std::array<NoDefaultConstructible, 10> ar2 = create_array<10>(NoDefaultConstructible(42));

    return 0;
}

Inspired by @Nawaz, this is more concise and makes better use of the standard: https://godbolt.org/z/d6a7eq8T5

#include <array>
#include <type_traits>

#include <fmt/format.h>
#include <fmt/ranges.h>

// Inspired by https://stackoverflow.com/a/18497366/874660 

//! Return a std::array<T, Sz> filled with
//! calls to fn(i) for i in the integer sequence:
template <typename Int, Int...N, typename Fn>
[[nodiscard]] constexpr auto transform_to_array(std::integer_sequence<Int, N...>, Fn fn) {
   return std::array{(static_cast<void>(N), fn(N))...}; 
}

//! Repeated application of nullary fn:
template <std::size_t N, typename Fn>
[[nodiscard]] constexpr auto generate_n_to_array(Fn fn) -> std::array<decltype(fn()), N> {
    return transform_to_array(std::make_integer_sequence<std::size_t, N>(), [&](std::size_t) { return fn(); });
}

int main() {
    static constexpr std::array<int, 3> a = generate_n_to_array<3>([i = 0]() mutable { return 2 * (i++); });
    fmt::print("{}\n", a);    
}

There are already many great answers, so a single remark: most of them use N twice: once in the array<T,N> and once in the repeat/genseq/generate/whatever you call it...

Since we are using a single value anyway, it seems we can avoid repeating the size twice, here's how:

#include <iostream>
#include <array>
#include <type_traits>

// Fill with a value -- won't work if apart from non-default-constructible, the type is also non-copyable...
template <typename T>
struct fill_with {
    T fill;
    constexpr fill_with(T value) : fill{value} {}

    template <typename... Args>
    constexpr fill_with(std::in_place_type_t<T>, Args&&... args) : fill{ T{std::forward<Args>(args)...} } {}

    template <typename U, size_t N>
    constexpr operator std::array<U, N> () { 
        return [&]<size_t... Is>(std::index_sequence<Is...>) {
            return std::array{ ((void)Is, fill)... };
        }(std::make_index_sequence<N>{});
    }
};


// A little more generic, but requires C++17 deduction guides, otherwise using it with lambdas is a bit tedious
template <typename Generator>
struct construct_with {
    Generator gen;
    template <typename F> constexpr construct_with(F && f) : gen{std::forward<F>(f)} {}

    template <typename U, size_t N>
    constexpr operator std::array<U, N> () { 
        return [&]<size_t... Is>(std::index_sequence<Is...>) {
            return std::array{ ((void)Is, gen())... };
        }(std::make_index_sequence<N>{});
    }
};

template <typename F>
construct_with(F&&) -> construct_with<F>;


struct A {
    A(int){}
    A(A const&) = delete;
};

int main() {
    std::array<int, 7> arr = fill_with<int>{ 7 };
    std::array<int, 7> arr = fill_with{ 7 }; // Ok since C++17
    // or you can create a maker function to wrap it in pre-c++17 code, as usual.
    std::array<A, 7> as = construct_with{[]{ return A{7}; }};
    for (auto & elem : arr) std::cout << elem << '\n';
}

Note: Here I used a few C++20 constructs, which may or may not be an issue in your case, but changing from one to the other is pretty straight-forward

I'd also recommend using <type_traits> and std::index_sequence and all the other sequence s from the std if possible to avoid reinventing the wheel)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM