Std :: copy from range to self

It may be a stupid question, and the answer may simply be “Because,” but I'm curious, so here it is. Suppose I want to copy the finite elements of std::vector to the foreground, not paying attention 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 reflect this undefined behavior if N == 0 (if only v.empty() , I suppose). Since even the naive implementation of std::copy will not have problems in this case (and will actually give NOP), why is the standard so strict?

+2
source share

All Articles