对于一个array来说:
For POD-types, a shallow copy or memcpy of the whole array is good enough, while for non-POD types, we need to perform element by element copies.
来自<Learning Boost C++ Libraries> 第296页.
示例代码:
#include <boost/type_traits/is_pod.hpp>
#include <cstring>
#include <string>
#include <iostream>
struct MyStruct {
int n;
float f;
const char *s;
};
template <typename T, size_t N>
T* fastCopy(T(&arr)[N], boost::true_type podType)
{
std::cerr << "fastCopy for POD\n";
T *cpyarr = new T[N];
memcpy(cpyarr, arr, N*sizeof(T));
return cpyarr;
}
template <typename T, size_t N>
T* fastCopy(T(&arr)[N], boost::false_type nonPodType)
{
std::cerr << "fastCopy for non-POD\n";
T *cpyarr = new T[N];
std::copy(&arr[0], &arr[N], &cpyarr[0]);
return cpyarr;
}
template <typename T, size_t N>
T*