我正在玩 Phoenix v3,试图弄清楚我们是否应该对其进行标准化,而不是当前的 Bind 和 Lambda 组合。从文档中我得到的印象是应该可以简化一些表达式。
目前,我坚持将 ->* 运算符与 STL 算法结合使用。以下将编译(Visual Studio 2008 SP1),但不会给出预期的输出:
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/mem_fn.hpp>
#include <boost/phoenix.hpp>
using namespace std;
using namespace boost;
using namespace boost::phoenix;
using namespace boost::phoenix::arg_names;
struct A
{
void f() const { cout << "A"; };
};
struct B
{
A a() { return A(); };
};
int main()
{
vector<A> va(1);
for_each(va.begin(), va.end(), bind(mem_fn(&A::f), arg1));
for_each(va.begin(), va.end(), arg1->*&A::f);
vector<B> vb(1);
for_each(vb.begin(), vb.end(), bind(mem_fn(&A::f), bind(mem_fn(&B::a), arg1)));
return 0;
}
运行此示例将打印两次“A”,两次都是基于绑定的循环。所以这是我的问题:
- 为了让基于运算符的循环真正调用 A::f,我应该更改什么?
- 如何使用运算符更改双绑定循环?
- 任何人都知道为什么在这些情况下不指定 mem_fn 时 VS2008 总是抱怨?我总是收到警告 C4180(应用于函数类型的限定符没有意义;被忽略)。
提前感谢您的任何见解。