Phoenix lambda 和参数解引用
有人可以告诉我如何使用 boost::phoenix lambda 实现与以下内容等效的内容吗? 我知道我可以通过许多其他方式实现它,但我正在尝试学习 Phoenix lambda 表达式,到目前为止我在此示例中的所有尝试都失败了。
我想要做的是使用 std::remove_if 迭代我的weak_ptrs 集并删除过期的。
所以代码看起来像这样:
typedef std::set<weak_ptr<Xyz>> XyzWptrSetType;
...
XyzWptrSetType xyzWptrSet;
...
XyzWptrSetType::iterator it =
std::remove_if(xyzWptrSet.begin(), xyzWptrSet.end(),
(boost::phoenix::arg_names::_1->expied()));
// the lambda part DOESN'T compile in previous line
std::erase(it, xyzWptrSet.end());
我发现的大多数 lambda 示例都非常简单,并且不处理在 lambda 参数对象上调用成员函数,尤其是当存在多个间接级别时。即 _1 预计代表集合的迭代器,通过使用“->”取消引用返回value_type(即weak_ptr),我想在其上调用expired。但由于 _1 实际上不直接是一个迭代器,而是一个“phoenix::actor”,所以我的取消引用无法编译。
感谢您提前提供的所有创意投入。
加布
Can somebody show me how to implement an equivalent of the following using boost::phoenix lambda?
I know I could implement it in many other ways but I'm trying to learn the Phoenix lambda expressions and so far all my attempts in this example failed.
What I want to do is to use the std::remove_if to iterate through my set of weak_ptrs and remove the expired ones.
So the code would look something like:
typedef std::set<weak_ptr<Xyz>> XyzWptrSetType;
...
XyzWptrSetType xyzWptrSet;
...
XyzWptrSetType::iterator it =
std::remove_if(xyzWptrSet.begin(), xyzWptrSet.end(),
(boost::phoenix::arg_names::_1->expied()));
// the lambda part DOESN'T compile in previous line
std::erase(it, xyzWptrSet.end());
Most of the lambda examples I've found are very simplistic and do not deal with calling a member function on a lambda argument object especially when there is more than one level of indirection. I.e. the _1 is expected to represent the set's iterator which by dereferencing with "->" returns value_type (being weak_ptr), on which I want to call expired. But since _1 is in fact not dirrectly an iterator but rather an "phoenix::actor", my dereferencing doesn't compile.
Thanks for all the creative inputs in advance.
Gabe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Boost.Phoenix 和之前的 Boost.Lambda 擅长某些任务,但不擅长其他任务。它实际上不起作用的其他问题之一是与直接按名称调用函数有关。
Boost.Phoenix lambda 可以快速、轻松地为重载运算符创建。但如果你需要一个函数名,那么你必须使用丑陋的语法:
这就是你的 lambda 的样子。您可以使用一些
using
指令来剪切命名空间,但这就是它最终的样子。此时,它实际上与使用boost::bind
没有什么不同:Boost.Phoenix 和之前的 Boost.Lambda 最适合使用重载运算符或显式定义的 Phoenix 操作对象的 lambda 表达式。如果您只有一个普通的旧函数或成员函数,则必须绑定它才能调用它。所以你也可以只使用
boost::bind
。Boost.Phoenix, and Boost.Lambda before it, excel at certain tasks, but not at others. One of those others that it doesn't really work at is anything to do with directly calling a function by name.
Boost.Phoenix lambdas can be quickly and easily created for overloaded operators. But if you need a function name, then you have to use ugly syntax:
That is what your lambda looks like. You can use some
using
directives to clip off namespaces, but that's ultimately what it's going to look like. It's really no different from usingboost::bind
at this point:Boost.Phoenix, and Boost.Lambda before it, are best used for lambda expressions that use overloaded operators, or explicitly-defined Phoenix action objects. If you just have a plain old function or member function, you have to bind it to call it. So you may as well just use
boost::bind
.Boost phoenix(和 boost lambda)不支持 ->操作员。您可以使用“指向成员的指针”运算符 (->*) 作为合理的替代方案。
我发现在使用 lambda 表达式的行之前将成员指针定义为单独的变量很有用:
正如其他人所指出的,对于像您这样的情况也值得考虑使用 bind() 。
Boost phoenix (and boost lambda) do not support the -> operator. You can use the "pointer to member" operator (->*) as a reasonable alternative.
I find it useful to define the member pointer as a separate variable immediately before the line that uses a lambda expression:
As others have noted, it is also worth considering bind() for situations like yours.