我的Boost Phoenix lambda出了什么问题?

时间:2014-06-29 16:47:19

标签: c++ boost lambda boost-phoenix

我认为Phoenix lambda函数不知何故是C ++ 11 lambda。所以我尝试以下方法:

http://coliru.stacked-crooked.com/a/38f1a2b655ea70fc

#include <boost/phoenix.hpp>
#include <iostream>
#include <ostream>

using namespace std;
using namespace boost;
using namespace phoenix;
using namespace arg_names;
using namespace local_names;


struct FakeOne{
    int field;    
};

int main()
{
    auto k = FakeOne();    

    auto fn = (lambda(_a=k)[_a.field ]);
   cout <<
      fn()
   << endl;

}

引发:

main.cpp:20:32: error: 'const _a_type' has no member named 'field'
     auto fn = (lambda(_a=k)[_a.field ]);

1 个答案:

答案 0 :(得分:1)

您不能只调用占位符(如_a)上的成员,因为它们不会声明成员(例如field)。相反,绑定它们:

auto fn = phx::bind(&FakeOne::field, k);

更新发表评论:

#include <boost/phoenix.hpp>

namespace phx = boost::phoenix;
using namespace phx::local_names;

struct FakeOne{
    int field;    
};

auto k = FakeOne { 3 };

int main()
{
    auto fn = phx::bind(&FakeOne::field, k);

    k.field = 99;
    return fn();
}

编译到

main:                     ; test.cpp:13
    movl    k(%rip), %eax ; boost/boost/proto/expr.hpp:65
    movl    $99, k(%rip)  ; test.cpp:16
    ret

在GCC -O3上