You can probably do something like the following. Depending on your compiler and the optimziation settings that you use you may get the effect that you are looking for.
Be aware that for small objects like char it may well be slower than a std::copy
or a memcpy
and that for larger objects the cost of a loop is likely to be insignificant compared to the copies going on in any case.
#include <cstddef>
template<std::size_t base, std::size_t count, class T, class U>
struct copy_helper
{
static void copy(T dst, U src)
{
dst[base] = src[base];
copy_helper<base + 1, count - 1, T, U>::copy(dst, src);
}
};
template<std::size_t base, class T, class U>
struct copy_helper<base, 0, T, U>
{
static void copy(T, U)
{
}
};
template<std::size_t base, std::size_t count, class T, class U>
void copy(T dst, U src)
{
copy_helper<base, count, T, U>::copy(dst, src);
}
template void copy<5, 9, char*, const char*>(char*, const char*);
#include <iostream>
#include <ostream>
int main()
{
const char test2[] = " , World\n";
char test[14] = "Hello";
copy<5, 9>(test, test2);
std::cout << test;
return 0;
}