简体   繁体   English

对std :: for_each实现的限制

[英]Restrictions on std::for_each implementation

In §25.2.4.2 of the C++ standard ( std::for_each ): 在C ++标准的§25.2.4.2中( std::for_each ):

template<class InputIterator, class Function>   Function
for_each(InputIterator first, InputIterator last, Function f);

Effects : Applies f to the result of dereferencing every iterator in the range [first,last), starting from first and proceeding to last - 1 . 效果 :将f应用于取消引用[first,last]范围内的每个迭代器的结果, 从第一个开始到最后一个 - 1

  • Does this mean that f is applied to the elements of a container in order ? 这是否意味着f 按顺序应用于容器的元素?
  • If so, does the parallel mode of libstdc++ violate it? 如果是这样, libstdc ++并行模式是否违反了它?
  • If not, why is the range-based for loop in §6.5.4 not implemented as a call to std::for_each? 如果没有,为什么§6.5.4中基于范围的for循环没有实现为对std :: for_each的调用? (this would allow range-based for loops to also be automatically parallelized by the implementation) (这将允许基于范围的for循环也由实现自动并行化)
  • Does this mean that f is applied to the elements of a container in order ? 这是否意味着f 按顺序应用于容器的元素?

I originally said no, but I think it does mean that, yes. 我最初说不,但我认为这确实意味着,是的。 Other algorithms don't include that specific wording. 其他算法不包括该特定措辞。

  • If so, does the parallel mode of libstdc++ violate it? 如果是这样,libstdc ++的并行模式是否违反了它?

Maybe, the parallel mode is an extension, and somewhat experimental, not really claiming to be a 100% conforming implementation of the standard library. 也许,并行模式是一个扩展,并且有点实验性,并不是真正声称是标准库的100%一致性实现。 (If it does claim that somewhere in the docs I'll fix the docs! ;-) (如果它确实声称文档中的某个地方我将修复文档!;-)

  • If not, why is the range-based for loop in §6.5.4 not implemented as a call to std::for_each? 如果没有,为什么§6.5.4中基于范围的for循环没有实现为对std :: for_each的调用? (this would allow range-based for loops to also be automatically parallelized) (这将允许基于范围的for循环也自动并行化)

Range-based for does not depend on the standard library to work. 基于范围for不依赖于标准库的工作。 If std::begin and std::end are visible they might be used, but are not required. 如果std::beginstd::end可见,则可以使用它们,但不是必需的。 Also, it would involve packaging up the loop body as a lambda so you have a function object to pass to std::for_each , which would complicate the specification of range-based for , which is supposed to have the same semantics and be exactly as efficient as a hand-written for loop. 此外,它将涉及将循环体打包为lambda,因此你有一个函数对象传递给std::for_each ,这会使基于范围的for的规范变得复杂,它应该具有相同的语义并且完全如同高效的手写for循环。 But the real reason might be that noone thought to do it that way! 但真正的原因可能是没有人想过这样做!

If not, why is the range-based for loop in §6.5.4 not implemented as a call to std::for_each? 如果没有,为什么§6.5.4中基于范围的for循环没有实现为对std :: for_each的调用? (this would allow range-based for loops to also be automatically parallelized) (这将允许基于范围的for循环也自动并行化)

Well, std::for_each is not allowed by the standard to be "automatically parallelized" (it must proceed sequentially, as stated in the standard), so there's that. 好吧, std::for_each 不允许 std::for_each “自动并行化”(它必须按顺序进行,如标准中所述),所以就是这样。 But more importantly, range-based for allows other things besides std::for_each . 但更重要的是,基于范围for允许,除了其他事情std::for_each As a language feature, you get to, for example, break out of the loop. 这样的语言特性,你得到,例如, break了循环。 You can use goto or other language constructs. 您可以使用goto或其他语言结构。 And so forth. 等等。

std::for_each is based around calling a function for each iteration. std::for_each基于为每次迭代调用一个函数。 And you can't really "break" out of a function. 你不能真正“打破”一个功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM