I'm trying to create a rule that returns a function<char(char const *)>
constructed by currying a Phoenix expression. E.g.,
start = int_[_val = xxx];
rule<Iterator, function<char(char const *)> start;
What should xxx
be so that parsing the string "5"
should give me a function that gives me the fifth character of its input? I've tried things like lambda(_a = arg1)[arg1[_a]](_1)
might work, but I've not been able to hit on the magic formula.
In other words, I'd like the attribute to curry arg2[arg1]
on the value of the parsed int
Very grateful for any suggestions. Note that I'm on VC2008, so C++11 lambdas not available.
Mike
After fixing that rule declaration:
it worked: Live On Coliru (c++03).
UPDATE:
Why did I end up with such a complex contraption?
Well. Let me tell you about the joy of complexing functional composition with lazy evaluation (in C++ template meta-programming that has these surprises with reference/value semantics): Don't do the following:
Depending on the compiler, optimization level, this might *appear to work. But it's invoking Undefined Behaviour [1]. The problem is that
qi::_1
will be kept as a reference to the attribute exposed byqi::int_
parser expression. However, this reference, after the lifetime of the parser context has ended, is a dangling reference.So evaluating the functor indirects through an invalid reference. To avoid this you should say (Live On Coliru):
or even (if you like obscure code):
Or, you know, you can stick with the bound nested lambda, since the bind defaults to value-semantics for
qi::_1
(unless you used thephx::cref
/phx::ref
wrappers).I hope the above analysis drives home the point I made in the comments earlier:
Prints
[1] (MSVC2013 appeared to crash, gcc may appear to work in -O3, but segfaults in -O0 etc.)