I wrote a class with a constexpr
copy constructor. (It is a struct in example to make it simpler.) One of the fields is an array. I want copy it too.
struct Foo
{
static constexpr int SIZE = 4;
constexpr Foo() = default;
constexpr Foo(const Foo &foo) :
arr{foo.arr[0], foo.arr[1], foo.arr[2], foo.arr[3]},
bar(foo.bar+1) {}
int arr[SIZE] = {0, 0, 0, 0};
int bar = 0;
};
My version works but it isn't scalable. If I change SIZE
, I have to modify the constructor. In addition, code looks ugly.
Is it any better way to copy array in constructor? Constructor must be constexpr
.
std::array
.) – Jorymake_index_sequence
and delegating to a template constructor that does a pack expansion.make_index_sequence
is C++14 but implementable in C++11. – Teaching