Why does std::array not have an constructor that takes a value for the array to be filled with?

前端 未结 4 1692
南方客
南方客 2020-11-30 05:38

Is the absence of

std::array::array(const T& value);

an oversight? It seems mighty useful to me, and dynamic containers (

相关标签:
4条回答
  • 2020-11-30 05:47

    You may use std::index sequence for that:

    namespace detail
    {
    
        template <typename T, std::size_t...Is>
        constexpr std::array<T, sizeof...(Is)>
        make_array(const T& value, std::index_sequence<Is...>)
        {
            return {{(static_cast<void>(Is), value)...}};
        }
    }
    
    template <std::size_t N, typename T>
    constexpr std::array<T, N> make_array(const T& value)
    {
        return detail::make_array(value, std::make_index_sequence<N>());
    }
    

    Demo

    std::make_index_sequence is C++14, but can be implemented in C++11.

    static_cast<void>(Is) is to handle evil operator, that T might provide.

    0 讨论(0)
  • 2020-11-30 05:48

    Note that you can efficiently simulate this type of constructor by taking advantage of the fact that array is not zero-initialized, and has a copy constructor and do.

    template <size_t N, class T>
    array<T,N> make_array(const T &v) {
        array<T,N> ret;
        ret.fill(v);
        return ret;
    }
    
    auto a = make_array<20>('z');
    
    0 讨论(0)
  • 2020-11-30 05:59

    std::array is, by design, an aggregate, so has no user-declared constructors.

    As you say, you could use fill after default constructing. Since it's an aggregate, default construction won't zero the memory, but will leave it uninitialised (if the contained type is trivially initialisable).

    0 讨论(0)
  • 2020-11-30 05:59

    First of all, it is not std::array<T>, it is std::array<T,N> where N is compile time constant integral expression.

    Second, std::array is made aggregate by design. So it doesn't have anything which makes it non-aggregate, which is why it doesn't have constructor... and destructor, virtual functions, etc.

    0 讨论(0)
提交回复
热议问题