C ++标准措辞:通过范围内的所有迭代器“隐含顺序性? [英] C++ standard wording: Does "through all iterators in the range" imply sequentiality?

查看:186
本文介绍了C ++标准措辞:通过范围内的所有迭代器“隐含顺序性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个SO问题引发了关于 std :: generate 的讨论,该标准的保证。特别是,你可以使用内部状态的函数对象并依靠生成(it1,it2,gen)调用 gen(),将结果存储在 * it ,再次调用 gen(),存储在

This SO question sparked a discussion about std::generate and the guarantees made by the standard. In particular, can you use function objects with internal state and rely on generate(it1, it2, gen) to call gen(), store the result in *it, call gen() again, store in *(it + 1) etc., or can it start at the back, for example?

标准(n3337,§25.3) .7 / 1)说:

The standard (n3337, §25.3.7/1) says this:


效果:第一个算法调用函数对象 gen ,并通过 [first,last] 范围内的所有迭代器分配gen的返回值。第二个算法调用函数对象gen并通过 [first,first + n)如果 n的所有迭代器分配gen的返回值

Effects: The first algorithm invokes the function object gen and assigns the return value of gen through all the iterators in the range [first,last). The second algorithm invokes the function object gen and assigns the return value of gen through all the iterators in the range [first,first + n) if n is positive, otherwise it does nothing.

看起来没有保证顺序,特别是因为其他段落具有更强的措辞,例如 std :: for_each 效果:应用 f last-1 开始,在范围 [first,last)如果我们从字面上来看,它只保证从开始开始,结束于 last 中间的顺序)。

It seems like no ordering is guaranteed, especially since other paragraphs have stronger wording, for example std::for_each (Effects: Applies f to the result of dereferencing every iterator in the range [first,last), starting from first and proceeding to last - 1. If we're taking this literally, it only guarantees to start at first and end at last though - no guarantees on the ordering in between).

Microsoft Apache的C ++标准库都提供了示例在需要评估是顺序的文档页面上。和libc ++(在算法)和libstdc ++(在 bits / stl_algo.h )实现它的方式。此外,在没有此保证的情况下,您会失去许多潜在的应用程序生成

But: Both Microsoft's and Apache's C++ standard library both give examples on their documentation pages that require the evaluation to be sequential. And both libc++ (in algorithm) and libstdc++ (in bits/stl_algo.h) implement it that way. Moreover, you lose a lot of potential applications for generate without this guarantee.

当前的措辞是否意味着顺序性?如果没有,这是委员会成员的监督还是有意的?

Does the current wording imply sequentiality? If not, was this an oversight by the members of the committee or intentional?

(我很清楚,没有很多人可以提供有洞察力的答案问题,而不仅仅是推测或讨论,但是我的愚见认为,这不会使这个问题不建设性根据SO准则。)

(I am well aware that there aren't many people who can provide insightful answers to this question without merely speculating or discussing, but in my humble opinion, this does not make this question 'not constructive' as per SO guidelines.)

感谢@juanchopanza指出这个问题,并指向我的 for_each

Thanks to @juanchopanza for pointing out this issue and referring me to the paragraph about for_each.

推荐答案

在讨论 LWG475 std :: for_each std :: transform 进行比较。注意 transform 不保证其函数对象被调用的顺序。所以,是的,委员会知道在标准中缺乏顺序保证。

In the discussion of LWG475, std::for_each is compared with std::transform. It's noted that "transform does not guarantee the order in which its function object is called". So, yes, the committee is aware of the lack of sequential guarantees in the standard.

非顺序行为没有相反的要求,因此Microsoft和Apache可以自由使用顺序评估。

There is no opposite requirement for non-sequential behavior either, so Microsoft and Apache are free to use sequential evaluation.

这篇关于C ++标准措辞:通过范围内的所有迭代器“隐含顺序性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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