2

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?

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431

2 Answers2

0

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 .

  • No, I make a mistake, the difference is the return type,the official one is return F, my own one is void, maybe this is the problem. – arthur.wang Jan 15 '12 at 15:58
0

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.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • Thanks for you reply.You mean the code in lambda is execution under undefined order, seams it's better write only one line in one lambda function. But according to phoenix online document, you can write more than one lines in one lambda function, if the order is undefined, I think it's almost useless for write more than one line. But what about if_ then_ statement, what's the order in these statement, I will try it later. Thanks – arthur.wang Jan 16 '12 at 02:46