1

Is there a way to construct a templated array of elements with all duplicate elements when the element type does not have a default constructor?

I tried the following:

template<typename T, int n> struct Array {
    template<typename... Args> explicit Array(const T& arg1, Args... args)
        : m_a{arg1, args...} { }
    static Array<T,n> all(const T& value) {
        Array<T,n> ar; // error: use of deleted function 'Array<TypeWithoutDefault, 10>::Array()'
        for (int i=0; i<n; i++)
            ar.m_a[i] = value;
        return ar;
    }
    T m_a[n];
};

struct TypeWithoutDefault {
    TypeWithoutDefault(int i) : m_i(i) { }
    int m_i;
};

int main() {
    // works fine
    Array<TypeWithoutDefault,2> ar1 { TypeWithoutDefault{1}, TypeWithoutDefault{2} };
    (void)ar1;
    // I want to construct an Array of n elements all with the same value.
    // However, the elements do not have a default constructor.
    Array<TypeWithoutDefault,10> ar2 = Array<TypeWithoutDefault, 10>::all(TypeWithoutDefault(1));
    (void)ar2;
    return 0;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
Hugues
  • 2,865
  • 1
  • 27
  • 39
  • 1
    Alright you need `repeat` functionality. Look at my answer here : [How to initialize std::array elegantly if T is not default constructible?](http://stackoverflow.com/questions/18497122/how-to-initialize-stdarrayt-n-elegantly-if-t-is-not-default-constructible) – Nawaz Jan 07 '14 at 07:36
  • Let me know if that works for you too. If not, I will modify it, and post it as answer. – Nawaz Jan 07 '14 at 07:38
  • Thanks, my question was unfortunately a duplicate one as pointed out by several people. The answers by @Nawaz and @Jarod42 are both very appropriate. The concise construction of `index_sequence` and `make_index_sequence` is very nice. – Hugues Jan 07 '14 at 21:09

1 Answers1

1

Following will solve your issue:

#if 1 // Not in C++11

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
        // and avoid possible "evil" overload of `operator ,` (comma).
        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 TypeWithoutDefault {
    TypeWithoutDefault(int i) : m_i(i) { }
    int m_i;
};

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

    return 0;
}
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • No need to use cast in `{{(static_cast(Is), value)...}};`. You could just use `{{(Is, value)...}};`. Keep it simple! – Nawaz Jan 08 '14 at 04:21
  • @Nawaz: In addition to the possibility of a warning, the cast avoids any user-defined `operator,` for `T`. – Davis Herring Jun 20 '23 at 08:24