phoenix::lambda函数的执行顺序是怎样的?
我是boost phoenix的新手,我写了一小段代码,但它完全让我对执行顺序感到困惑,你可以检查代码
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);
第一个输出是“ab” 但第二个输出是“dbca”
我犯了一些错误吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是第二个 ph::for_each ,如果我将其替换为我自己的版本 for_each ,它会从左到右工作。
我将我自己的版本与官方版本进行比较,区别在于它使用了Detail::begin和detail::end。
但真正的问题是什么,我将继续进一步调查。
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 .
它认为这可以用通用函数参数求值的未定义顺序来解释。 Phoenix Lambda 语法可能会简化为某种形式的函数调用。
标准没有提到这必须是什么顺序,因此编译器实现者可以自由地按照他们的意愿去做。您不能依赖函数参数求值顺序。
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.