V
Victor Bazarov
Below you will find some code I wrote to see if I could wrap array copying
(especially for multi-dimensional arrays) in a simple class and/or
function. It seems to work fine for one-dimensioned arrays as well as
two-dimensioned ones. I am sure three- or more-dimensioned array are just
as OK here.
My concern was that I couldn't use 'std::copy' to copy multi-dimensional
arrays. Perhaps in the future we'll see specialisations of 'std::copy'
function that will work with multidimensional arrays similarly to the
'arrcpy' function. [I intentionally named 'arrcpy' that way to indicate
that the arguments are similar to 'strcpy', first the destination, then
the source.]
I've not managed to make this code accept a constant array (or array of
const T), simply because it would probably require a cast for one of the
placement new operators. But in most cases it doesn't matter. Extend it
if you need to.
I am putting it here because I think you might want to comment on it. If
you think you could use it, use it. If you think you have seen something
like this before, do mention it. I am not claiming any innovation here,
just my laziness to look for something similar that has been already done.
Regards,
Victor
cut here 8<-------------------------------------------------- >8 cut here
#include <memory> // for placement new
template<class T, size_t N> struct AC
{
struct W
{
T arr[N];
};
AC(T (*ar1)[N], T (*ar2)[N]) : w1(new (ar1) W), w2(new (ar2) W) {}
~AC() { *w1 = *w2; }
W *w1, *w2;
};
template<class T, size_t N> void arrcpy(T (&ar1)[N], T (&ar2)[N])
{
AC<T,N> ac(&ar1, &ar2);
}
int main()
{
int ai[1000] = { 1,2,3 };
int bi[1000] = { 5,6,7 };
double a[100][10] = { 1,2,3 };
double b[100][10] = { 5,6,7 };
arrcpy(ai, bi);
arrcpy(a, b);
return 0;
}
cut here 8<-------------------------------------------------- >8 cut here
(especially for multi-dimensional arrays) in a simple class and/or
function. It seems to work fine for one-dimensioned arrays as well as
two-dimensioned ones. I am sure three- or more-dimensioned array are just
as OK here.
My concern was that I couldn't use 'std::copy' to copy multi-dimensional
arrays. Perhaps in the future we'll see specialisations of 'std::copy'
function that will work with multidimensional arrays similarly to the
'arrcpy' function. [I intentionally named 'arrcpy' that way to indicate
that the arguments are similar to 'strcpy', first the destination, then
the source.]
I've not managed to make this code accept a constant array (or array of
const T), simply because it would probably require a cast for one of the
placement new operators. But in most cases it doesn't matter. Extend it
if you need to.
I am putting it here because I think you might want to comment on it. If
you think you could use it, use it. If you think you have seen something
like this before, do mention it. I am not claiming any innovation here,
just my laziness to look for something similar that has been already done.
Regards,
Victor
cut here 8<-------------------------------------------------- >8 cut here
#include <memory> // for placement new
template<class T, size_t N> struct AC
{
struct W
{
T arr[N];
};
AC(T (*ar1)[N], T (*ar2)[N]) : w1(new (ar1) W), w2(new (ar2) W) {}
~AC() { *w1 = *w2; }
W *w1, *w2;
};
template<class T, size_t N> void arrcpy(T (&ar1)[N], T (&ar2)[N])
{
AC<T,N> ac(&ar1, &ar2);
}
int main()
{
int ai[1000] = { 1,2,3 };
int bi[1000] = { 5,6,7 };
double a[100][10] = { 1,2,3 };
double b[100][10] = { 5,6,7 };
arrcpy(ai, bi);
arrcpy(a, b);
return 0;
}
cut here 8<-------------------------------------------------- >8 cut here