std::copy from a range into itself

2019-05-23 02:01发布

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?

0条回答
登录 后发表回答