The gcc compiler generates call to memcpy when i add __restrict to function parameters.
How does compiler/standard library figure out that it can generate calls to memcpy when appropriate?
void call_stdcpy_r(int *__restrict p, int *__restrict q, int sz) {
std::copy(p, p+sz, q); // generates call to memcpy
}
void call_stdcpy(int *p, int *q, int sz) {
std::copy(p, p+sz, q); // generates call to memmove
}
As per https://en.cppreference.com/w/cpp/algorithm/copy
The behavior is undefined if the source and the destination ranges overlap.
Shouldn't the compiler, in principle, generate calls to memcpy all the time?
Link to godbolt: https://godbolt.org/z/aKj3Y5K8M