tower120
tower120

Reputation: 5255

What's wrong with my Boost Phoenix lambda?

I consider that Phoenix lambda functions is somehow C++11 lambda. So I try the following:

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 ]);

Upvotes: 1

Views: 160

Answers (1)

sehe
sehe

Reputation: 393114

You can't just invoke members on placeholders (like _a) since they don't declare the members (like 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

Upvotes: 1

Related Questions