What's the execution sequence of phoenix::lambda function?

168 Views Asked by At

I'm a newbie for boost phoenix, I wrote a small piece of code but it totally confuses me about the execution sequence, you can check the code

std::vector<int> v;
v.push_back(1);
ph::for_each(v,
             ph::lambda[ph::ref(cout)<<"a",
                        ph::ref(cout)<<"b"
                 ])(v);


ph::for_each(arg1,
             ph::lambda[ph::ref(cout)<<"a",
                        ph::for_each(v,
                                         ph::lambda[ph::ref(cout)<<"b",
                                                    ph::ref(cout)<<"c"
                                             ]),
                        ph::ref(cout)<<"d"
                 ])(v);

The first output is "ab" but the second output is "dbca"

Did I make some mistake?

2

There are 2 best solutions below

1
On

The problem is the second ph::for_each, if I replace it with my own version for_each, it works from left to right. I compare my own version with official one, the difference is it use detail::begin and detail::end . But what's is real problem, I will continue to investigate it more .

1
On

It think this can be explained under the undefined order in which general function arguments are evaluated. The phoenix Lambda syntax probably reduces to some form of function calls.

The Standard does not mention which order this has to be, and so compiler implementors are free to do as they wish. You cannot rely on function argument evaluation order.