std::copy from a range into itself

喜欢而已 提交于 2020-01-02 17:18:05

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!