I noticed that the following code did not optimize to a single memcpy, unlike I would expect:
template <class T>
void relocate_1(T *first, T *last, T *dest) {
for ( ; first != last; ++first, ++dest) {
std::memcpy((void*)dest, first, sizeof(T));
}
}
I would expect this to be equivalent to roughly:
template <class T>
void relocate_2(T *first, T *last, T *dest) {
auto n = last - first;
std::memcpy((void*)dest, first, n);
}
Is this a problem with e.g. the lack of knowledge that the [first, last) range is all valid? Note that both GCC and Clang fail to perform this optimization.
Godbolt: https://godbolt.org/z/zzdhcKPh4