哪些算法要求函子是纯函数? [英] Which algorithms require functors to be pure functions?
问题描述
通常,该标准要求函子是纯函数,因为允许算法将其函子复制到其内心的内容.但是,对于某些算法(例如find_if
),没有明智的实现会执行任何形式的仿函数复制.
我们可以依靠吗?
我 您要问的是哪些函子由于在任意时间被复制而需要是无状态的.>
我想不出任何需要使用自由函数的算法,但是大多数/全部当然都需要对象本身不保持状态.您可以 做的是让对象具有对状态对象的引用,该对象保存在算法调用之外.然后,仿函数的任何副本都可以适当地修改该状态对象.
Generally the standard requires functors to be pure functions because algorithms are allowed to copy their functors to their heart's content. However, there are some algorithms (e.g. find_if
) for which no sane implementation would be doing any form of functor copying.
Can we rely on this?
I think what you're trying to ask is which functors need to be stateless due to being copied at arbitrary times.
I can't think of any algorithms that require free functions to be used, but most/all certainly require the object itself to not hold state. What you can do is have the object have a reference to a state object that's held outside the algorithm call. Any copy of the functor can then modify that state object appropriately.
这篇关于哪些算法要求函子是纯函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!