Search code examples
c++arraystemplatesc++11initialization

templated array of duplicate elements without default constructor


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

Solution

  • 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;
    }