问题
This might be a silly question and the answer may simply be "Because," but I'm curious so here it goes. Suppose I want to copy the final elements of an std::vector
to the front without regard to what happens elsewhere. I can do this with a simple function call
#include <vector>
#include <algorithm>
std::vector<int> v;
.
.
.
std::copy(v.begin() + N, v.end(), v.begin());
What I find surprising is that the standard seems to brand this undefined behavior if N == 0
(unless v.empty()
, I suppose). Since even a naive implementation of std::copy
won't have problems in that case (and in fact yield a NOP), why is the standard so strict?
来源:https://stackoverflow.com/questions/32940331/stdcopy-from-a-range-into-itself