加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 应该std :: copy()或std :: move()的空范围需要有效的目的

发布时间:2020-12-16 03:38:38 所属栏目:百科 来源:网络整理
导读:以下代码中的std :: move()会在Visual Studio 2013(使用Debug配置)中编译时发出运行时警告,因为它会检测到dest为nullptr.但是,源范围是空的,所以dest不应该被访问. C标准可能不清楚是否应该允许? 它规定:要求:结果不得在[第一,最后一个]范围内. 一个nullp
以下代码中的std :: move()会在Visual Studio 2013(使用Debug配置)中编译时发出运行时警告,因为它会检测到dest为nullptr.但是,源范围是空的,所以dest不应该被访问.
C标准可能不清楚是否应该允许?
它规定:要求:结果不得在[第一,最后一个]范围内.
一个nullptr似乎可以满足这个要求.
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec;
    int* dest = nullptr;
    // The range [begin(vec),end(vec)) is empty,so dest should never be accessed.
    // However,it results in an assertion warning in VS2013.
    std::move(std::begin(vec),std::end(vec),dest);
}

解决方法

不仅需要满足Requires:子句,还需要满足效果:和返回:子句中的所有内容.我们来看看他们:

Effects: Copies elements in the range [first,last) into the range [result,result + (last - first)) starting from first and
proceeding to last.

作为第一个== last,那么范围[result,result 0]必须是一个有效的范围.

[iterator.requirements.general] / p7状态:

A range [i,i) is an empty range; … Range [i,j) is valid if and only if j is reachable from i.

同一节的p6说明:

An iterator j is called reachable from an iterator i if and only
if there is a finite sequence of applications of the expression ++i
that makes i == j.

从这些段落我得出以下结论:

int* dest = nullptr;

然后,[dest,dest]形成一个有效的空范围.所以效果:段落的第一句话对我来说似乎很好:

For each non-negative integer n < (last - first),performs *(result + n) = *(first + n).

没有非负整数n < 0,因此不能执行任务.所以第二句不禁止dest == nullptr.

Returns: result + (last - first).

[expr.add] / p8具体允许一个向任何指针值添加0,结果比较等于原始指针值.因此,dest 0是等于nullptr的有效表达式. Returns:子句没有问题.

Requires: result shall not be in the range [first,last).

我认为没有合理的方式来解释dest将是“空”.

Complexity: Exactly last - first assignments.

这证实没有任何作业可以完成.

我可以在标准中找不到任何声明,使这个例子除了形式很好.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读