简体   繁体   English

我的Boost Phoenix lambda有什么问题?

[英]What's wrong with my Boost Phoenix lambda?

I consider that Phoenix lambda functions is somehow C++11 lambda. 我认为Phoenix lambda函数在某种程度上是C ++ 11 lambda。 So I try the following: 因此,我尝试以下操作:

http://coliru.stacked-crooked.com/a/38f1a2b655ea70fc 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;

}

Which throws: 哪个抛出:

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

You can't just invoke members on placeholders (like _a) since they don't declare the members (like field ). 您不能仅在占位符上调用成员(如_a),因为它们没有声明成员(如field )。 Instead, bind them: 而是绑定它们:

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

Update To the comment: 更新到评论:

#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();
}

Compiles down to 编译成

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

on GCC -O3 在GCC -O3上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM