有没有办法摆脱 boost::mpl for_each ?
确实很简单的问题,让我给出一些背景知识:
我有一个 mpl::vector
类型,其中每种类型都有一个 id,在运行时我使用 mpl::for_each
迭代此向量并找到给定 id 的匹配类型。但一旦找到,继续循环就没有意义了,所以 - 问题是,有没有办法打破它(不抛出异常)?
Simple question really, let me give some background:
I have a mpl::vector
of types where each type has an id, at run time I use the mpl::for_each
to iterate through this vector and find the matching type for the given id. But once found, there is no point in continuing the loop, so - question is, is there a way to break out of it (without throwing an exception)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了实现类似
find_if
的功能,我更改了 for_each(将其称为exec_if
)以采用bool
模板参数。bool
指示是否应该执行下一个序列,或者是否提前返回。我将其更改为 exec_if ,因此现在当谓词匹配时,将使用该类型触发要执行的函子 - 这正是我所需要的。
To implement something like
find_if
I changed the for_each (calling itexec_if
) to take abool
template argument. Thebool
indicates if execution should with the next sequences, or in affect return early.I changed this to
exec_if
, so now when the predicate matches, then the functor to execute will be triggered with the type - this does exactly what I need.不,没有办法“破坏”
mpl::for_each
。话虽这么说,我可能误解了你的问题,但在我看来你需要mpl::find_if
多于mpl::for_each
:Nope, there is no way to 'break' a
mpl::for_each
. That being said, I might have misunderstood your problem, but it seems to me you needmpl::find_if
more thanmpl::for_each
: