是否定义了C ++标准算法的空白范围? [英] Is it defined to provide an empty range to C++ standard algorithms?

查看:75
本文介绍了是否定义了C ++标准算法的空白范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接下来是我的上一个问题我们能否证明标准允许我们将空范围传递给标准算法?

Following on from my previous question, can we prove that the standard allows us to pass an empty range to a standard algorithm?

第24.1/7段将空范围"定义为范围[i,i)(其中i是有效的),并且i看起来像是可到达的",但是我不确定有资格作为证明.

Paragraph 24.1/7 defines an "empty range" as the range [i,i) (where i is valid), and i would appear to be "reachable" from itself, but I'm not sure that this qualifies as a proof.

尤其是在查看排序功能时遇到麻烦.例如,std::sort:

In particular, we run into trouble when looking at the sorting functions. For example, std::sort:

复杂度:O(N log(N))(其中N == last-first)比较

Complexity: O(N log(N)) (where N == last - first) comparisons

由于通常认为log(0)是未定义的,而且我不知道0*undefined是什么,所以这里可能有问题吗?

Since log(0) is generally considered to be undefined, and I don't know what 0*undefined is, could there be a problem here?

(是的,好的,我有点书呆子.当然,没有自尊的 stdlib 实现不会导致将空范围传递给std::sort的实际问题.但是我想知道这里的标准措词是否有潜在的漏洞.)

(Yes, ok, I'm being a bit pedantic. Of course no self-respecting stdlib implementation would cause a practical problem with an empty range passed to std::sort. But I'm wondering whether there's a potential hole in the standard wording here.)

推荐答案

我似乎没有什么问题.在§24.1/6中,我们被告知:

I don't seem much room for question. In §24.1/6 we're told:

当且仅当存在++ i表达式的有限应用序列使i == j时,迭代器j才可从迭代器i调用.

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.

以及$ 24.1/7:

and in $24.1/7:

仅当j可以从i到达时,范围[i,j)才有效.

Range [i, j) is valid if and only if j is reachable from i.

由于0是有限的,所以[i, i)是有效范围. §24.1/7继续说:

Since 0 is finite, [i, i) is a valid range. §24.1/7 goes on to say:

将库中的函数应用到无效范围的结果是 未定义.

The result of the application of functions in the library to invalid ranges is undefined.

并不能说有效范围可以保证定义的结果(合理,因为还有其他要求,例如比较功能),但是肯定暗示范围本身是空的,不应导致UB或类似问题.但是,特别是,该标准将空范围设为另一个有效范围.空值范围和非空值范围之间没有真正的区别,因此适用于非空值有效范围的情况同样适用于空值有效范围.

That doesn't go quite so far as to say that a valid range guarantees defined results (reasonable, since there are other requirements, such as on the comparison function) but certainly seems to imply that a range being empty, in itself, should not lead to UB or anything like that. In particular, however, the standard makes an empty range just another valid range; there's no real differentiation between empty and non-empty valid ranges, so what applies to a non-empty valid range applies equally well to an empty valid range.

这篇关于是否定义了C ++标准算法的空白范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆