提升凤凰编译错误

时间:2014-02-04 10:11:34

标签: c++ compiler-errors boost-phoenix

我正在尝试使用Boost Phoenix。我的目标是让stl算法采用容器而不是迭代器范围,如上所述 here。 但是,我在一个相当简单的代码上得到了一堆错误:

#include <boost/phoenix/stl/algorithm.hpp>
#include <iostream>
#include <vector>
#include <algorithm>

namespace phx = boost::phoenix;

int main(int argc, char ** argv) {
    std::vector<int> my_arr{ 0, 1, 2, 3 };
    phx::for_each(my_arr, [](int i){std::cout << i << std::endl; });
}

我在两个平台(Win7,Ubuntu)上尝试过,否则我会使用Boost的多个部分。

错误消息很长,所以我把它们放在文件中:

除了模板没有正确编译之外,我无法真正做出很多错误,但我想我错过了一些相当简单的事情。

2 个答案:

答案 0 :(得分:4)

我认为您使用的是错误的boost库。凤凰算法是惰性函数,解释为here。因此phoenix::for_each如果被调用则不会执行任何操作,但返回一个函数对象,一旦调用它就会遍历该范围。如果您只想在范围上使用STL(和其他)算法,可以使用boost.range库:

#include <boost/range/algorithm/for_each.hpp>
#include <iostream>
#include <vector>

namespace rng = boost::range;

int main(int argc, char ** argv) {
    std::vector<int> my_arr{ 0, 1, 2, 3 };
    rng::for_each(my_arr, [](int i){std::cout << i << std::endl; });
}

答案 1 :(得分:1)

在包含任何其他内容之前,您只需要包含phoenix核心。

#include <boost/phoenix/core.hpp>
#include <boost/phoenix/stl/algorithm.hpp>
... rest of your program