没有默认构造函数的重复元素的模板化数组

templated array of duplicate elements without default constructor

本文关键字:数组 元素 默认 构造函数      更新时间:2023-10-16

当元素类型没有默认构造函数时,有没有办法用所有重复元素构造一个模板化的元素数组?

我尝试了以下方法:

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

以下内容将解决您的问题:

#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>());
}

所以测试一下:

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