Suppose I have a type A
with no default constructor:
struct A
{
int x;
A(int x) : x(x) {}
};
I want to make an std::
I think, your worries about code bloat are misconstrued. Here is a sample:
#include
#include
template
constexpr auto generate(std::index_sequence ) {
return std::array{(ix * ix)...};
}
std::array check() {
return generate(std::make_index_sequence<3>());
}
std::array glob = generate(std::make_index_sequence<5>());
It produces very neat assembly:
check():
movl $0, -24(%rsp)
movl $1, -20(%rsp)
movl $4, %edx
movq -24(%rsp), %rax
ret
glob:
.long 0
.long 1
.long 4
.long 9
.long 16
As you see, no code bloat in sight. Static array is statically initialized, and for automatic array it just a bunch of moves. And if you think bunch of moves is a dreaded code bloat, consider it loop unrolling - which everybody loves!
By the way, there is no other conforming solution. Array is initialized using aggregate initialization at construction time, so elements should either be default constructible or be initialized.