Вот фрагмент, который примерно соответствует той функциональности, которую я ищу
Код: Выделить всё
#include
#include
#include
struct not_default_constructible
{
not_default_constructible(int){}
};
static_assert(!std::is_default_constructible_v);
/*
Does not compile!
*/
template
struct array_of_ndc
{
std::array arr;
array_of_ndc(T init)
{
for(size_t i = 0; i < N; i++)
{
arr[i] = init;
}
}
};
int main()
{
array_of_ndc ndc(not_default_constructible{2});
}
Вот одно решение, которое я написал, но такое ощущение, будто я борюсь с компилятором (и оно ломает все, что мне нравится в std::array, то есть итераторы) , и я хотел посмотреть, есть ли более подходящее для STL решение.
Код: Выделить всё
template
struct no_construction_arr
{
alignas(T) char data[N * sizeof(T)];
template
requires(std::constructible_from)
no_construction_arr(U&& init)
{
for(size_t i = 0; i < N; i++)
{
new (&data[i * sizeof(T)]) T (init);
}
}
T* operator[](size_t index)
{
return reinterpret_cast(&data[index * sizeof(T)]);
}
~no_construction_arr()
{
for(size_t i = 0; i < N; i++)
{
(reinterpret_cast(&data[i * sizeof(T)]))->~T();
}
}
};
Подробнее здесь: https://stackoverflow.com/questions/784 ... -that-cant