Arrays cannot be copied.
int a[3];
int b[3];
a = b;
Further, when you pass an array to a function, its name decays to a pointer, therefore it is S(double arr[1])
equivalent S(double* arr)
. When you are inside the function, you need to copy the individual elements, so you also need the size of the array:
S(double *x, std::size_t sz) {
std::copy_n(x, sz, arr);
}
You can omit the size if you write the template as a function:
template <std::size_t sz)
S(double (&x)[sz]) {
std::copy_n(x, sz, arr);
}
Or, even better, use std::array
one that works as you expect.
source
share