1

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 ]);
tower120
  • 5,007
  • 6
  • 40
  • 88
  • 1
    *"I consider that Phoenix lambda functions is somehow C++11 lambda"* No they aren't. They use placeholders to substitute values and generate expressions. But they are not a language extension, so note what the compiler says: The placeholder `_a` has no member `field`, which makes perfect sense. – Manu343726 Jun 29 '14 at 18:20
  • @Manu343726 I pass `k` to lambda in the wrong way? – tower120 Jun 29 '14 at 18:48

1 Answers1

1

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

sehe
  • 374,641
  • 47
  • 450
  • 633