C++11 initialize array with uniform value in constexpr function

c++, c++11, constexpr

Solution

Rather curiously, a solution for the problem exists in c++14 (compile the example with `-std=c++1y` on gcc; also see comments for a bit more verbose c++11 solution by Praetorian):

template <size_t N>
struct bar {
    template <typename T, typename ...Tn>
    static constexpr auto apply(T v, Tn ...vs)
    {
        return bar<N - 1>::apply(v, v, vs...);
    }
};

template <>
struct bar<1> {
    template <typename T, typename ...Tn>
    static constexpr auto apply(T v, Tn ...vs)
    {
        return std::array<T, sizeof...(vs) + 1>{v, vs...};
    }

};

template <typename T, size_t N>
struct foo {
    std::array<T, N> data;

    constexpr foo(T val)
    : data(bar<N>::apply(val))
    {}
};

(I replaced the POD array with std::array - suppose it should not make any problems for your use case).

Live example is here: http://coliru.stacked-crooked.com/a/4731a10ee54563b9

Problem

I have a class template which builds a simple array based on the template parameters as one of its members. I need to be able to initialize every element in the array to a single value in one of the constructors. Unfortunately this constructor must be `constexpr`. The relevant part boils down to: ``` template <typename T, size_t N> class foo { T data[N]; constexpr foo(T val) { // initialize data with N copies of val } }; ``` Using `std::fill` or a loop is incompatible with the `constexpr` requirement. Initializing with `: data{val}` only sets the first element of the array and zero-initializes the remainder. How can this be achieved? I feel like there should be a solution with variadic templates and tuples etc...

Original source