Assign with nullptr and zero size

2020-07-18 06:41发布

If I e.g. have a vector v, is it ok by the standard to call:

void setData(const uint8_t* p, size_t s) {
    v.assign(p, p+s);
}

setData(nullptr, 0);

In general is it always ok to call STL functions XXX(InputIterator first, InputIterator last)as above when distance(first,last) == 0?

标签: c++ stl
2条回答
甜甜的少女心
2楼-- · 2020-07-18 07:22

In addition to what Potatoswatter stated, C++ also made the expression nullptr + 0 valid. This wasn't the case in C.

查看更多
SAY GOODBYE
3楼-- · 2020-07-18 07:41

Yes. It's OK to copy the nullptr value to another pointer object, and that's all the implementation could possibly do. It's already forbidden to go past the end (defined here by beginning == end) or dereference it.

查看更多
登录 后发表回答