In C ++, we often encounter the need to copy an object array, such as the following structure:
struct STest{ int a; int b; vector<int> vctInt;};
We have defined two Arrays:
STest A[20];STest B[20];
To copy all the content in array a to array B, we usually do this:
for(size_t i = 0; i < ARRAYSIZE(A); ++i){ A[i] = B[i];}
Memcpy cannot be used directly here, because there is a vector type in stest. However, if we define an array of built-in types, the above Code is less efficient, and the direct use of memcpy is more efficient.
To solve the problem above, we can use the C ++ template to process different types. There are two ways to write:
The first method is the easiest way to think of. It is to write a template directly and perform special processing on the C ++ built-in type of the template:
template<typename T,size_t N>inline void ArrayAssign(T (&A)[N], T (&B)[N]){ for(size_t i = 0; i < N; ++i) A[i] = B[i];}template<size_t N>inline void ArrayAssign(bool (&A)[N], bool (&B)[N]){ memcpy(A,B,sizeof(A));}template<size_t N>inline void ArrayAssign(char (&A)[N], char (&B)[N]){ memcpy(A,B,sizeof(A));}template<size_t N>inline void ArrayAssign(short (&A)[N], short (&B)[N]){ memcpy(A,B,sizeof(A));}template<size_t N>inline void ArrayAssign(int (&A)[N], int (&B)[N]){ memcpy(A,B,sizeof(A));}template<size_t N>inline void ArrayAssign(long long (&A)[N], long long (&B)[N]){ memcpy(A,B,sizeof(A));}template<size_t N>inline void ArrayAssign(unsigned char (&A)[N], unsigned char (&B)[N]){ memcpy(A,B,sizeof(A));}template<size_t N>inline void ArrayAssign(unsigned short (&A)[N], unsigned short (&B)[N]){ memcpy(A,B,sizeof(A));}template<size_t N>inline void ArrayAssign(unsigned int (&A)[N], unsigned int (&B)[N]){ memcpy(A,B,sizeof(A));}template<size_t N>inline void ArrayAssign(unsigned long long (&A)[N],unsigned long long (&B)[N]){ memcpy(A,B,sizeof(A));}
The second method is as follows. This method puts forward the judgment of the built-in type, which can be used in other places:
template<bool A,typename B,typename C>struct _if{};template<typename B,typename C>struct _if<true,B,C>{ typedef B Type; };template<typename B,typename C>struct _if<false,B,C>{ typedef C Type; };template<typename T,size_t N>inline size_t ARRAYSIZE(T (&)[N]){ return N;}template<typename T>struct IsInnerType { static const bool Value = false; };template<>struct IsInnerType<bool> { static const bool Value = true; };template<>struct IsInnerType<char> { static const bool Value = true; };template<>struct IsInnerType<short> { static const bool Value = true; };template<>struct IsInnerType<int> { static const bool Value = true; };template<>struct IsInnerType<long long> { static const bool Value = true; };template<>struct IsInnerType<unsigned char> { static const bool Value = true; };template<>struct IsInnerType<unsigned short> { static const bool Value = true; };template<>struct IsInnerType<unsigned int> { static const bool Value = true; };template<>struct IsInnerType<unsigned long long> { static const bool Value = true; };template<typename T,size_t N>struct ArrayAssignInnerType{ static inline void Invoke(T (&A)[N], T (&B)[N]) { memcpy(A,B,sizeof(A)); }};template<typename T,size_t N>struct ArrayAssignCustomType{ static inline void Invoke(T (&A)[N], T (&B)[N]) { for(size_t i = 0; i < N; ++i) A[i] = B[i]; }};template<typename T,size_t N>inline void ArrayAssign(T (&A)[N], T (&B)[N]){ _if<IsInnerType<T>::Value,ArrayAssignInnerType<T,N>,ArrayAssignCustomType<T,N> >::Type::Invoke(A,B);}
After the above processing, we don't have to worry about efficiency. At the same time, it is much more convenient to write, and there is no error. Simply write as follows:
ArrayAssign(B,A);
The above code is compiled in VC and GCC.