C++ 如何在Boost.Phoenix中为每个_使用基于范围的设置? 让我们考虑下面的代码: #include <boost/phoenix.hpp> #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> const small_ints {1, 2, 3, 4, 5}; std::vector<int> const big_ints {11, 12, 13, 14, 15}; namespace phoenix = boost::phoenix; using namespace boost::phoenix; using namespace boost::phoenix::placeholders; std::vector<int>::const_iterator big_ints_it; std::for_each(small_ints.cbegin(), small_ints.cend(), for_(phoenix::ref(big_ints_it) = begin(phoenix::cref(big_ints)), phoenix::ref(big_ints_it) != end(phoenix::cref(big_ints)), ++phoenix::ref(big_ints_it)) [ std::cout << val('(') << arg1 << ',' << *phoenix::ref(big_ints_it) << "), " ] ); }

C++ 如何在Boost.Phoenix中为每个_使用基于范围的设置? 让我们考虑下面的代码: #include <boost/phoenix.hpp> #include <algorithm> #include <iostream> #include <vector> int main() { std::vector<int> const small_ints {1, 2, 3, 4, 5}; std::vector<int> const big_ints {11, 12, 13, 14, 15}; namespace phoenix = boost::phoenix; using namespace boost::phoenix; using namespace boost::phoenix::placeholders; std::vector<int>::const_iterator big_ints_it; std::for_each(small_ints.cbegin(), small_ints.cend(), for_(phoenix::ref(big_ints_it) = begin(phoenix::cref(big_ints)), phoenix::ref(big_ints_it) != end(phoenix::cref(big_ints)), ++phoenix::ref(big_ints_it)) [ std::cout << val('(') << arg1 << ',' << *phoenix::ref(big_ints_it) << "), " ] ); },c++,boost,boost-phoenix,C++,Boost,Boost Phoenix,这是双迭代的笛卡尔积 但是的很难看!看看所有那些ref,cref(不知何故,我总是要从phoenix中明确选择,否则与std版本会有冲突)。更不用说我必须保持无用的big\u ints\u it变量 还要注意的是,这本身就是如此 现在我尝试对每个使用,希望得到更简单的版本: std::for_each(small_ints.cbegin(), small_ints.cend(), for_each(phoenix::cref(big_ints), std::cout &l

这是双迭代的笛卡尔积

但是的
很难看!看看所有那些
ref
cref
(不知何故,我总是要从
phoenix
中明确选择,否则与
std
版本会有冲突)。更不用说我必须保持无用的
big\u ints\u it
变量

还要注意的是,这本身就是如此

现在我尝试对每个
使用
,希望得到更简单的版本:

std::for_each(small_ints.cbegin(), small_ints.cend(),
    for_each(phoenix::cref(big_ints),
        std::cout << val('(') << arg1 << ", " << arg2 << "), "
    )
);
std::for_each(small_ints.cbegin(),small_ints.cend(),
对于每个人(凤凰城::cref(大国际),
std::cout with
1>          [
1> Sig=boost::proto::functional::at(boost::phoenix::vector2&,boost::mpl::int)
1>          ]
…

我甚至无法输入第一个错误,因为StackOverflow说,问题正文限制为30000个字符,我已经输入了135034个字符…

问题是您在内部表达式中使用了
arg2
。让我们看看情况

你基本上有:

- for_each(begin,end,outer_expr);
- outer_expr=for_each(begin,end,inner_expr);
outer_expr
internal_expr
都是lambda表达式,采用单个参数(其各自向量的一个元素)

您需要的是一种将参数从
外部expr
传递到
内部expr
的方法。幸运的是,Boost.Phoenix提供了一种简单的方法:

std::for_each(small_ints.cbegin(),small_ints.cend(),
phx::对于每个(phx::cref(大整数),

phx::lambda(_a=arg1)[phx::ref(std::cout)哇!现在我读了你的答案,我想知道我怎么可能没有看到
arg2
的明显问题!我在想什么?!谢谢!哦!至于编译器和实际的lambdas,你显然是对的。然而,对于我的最后工作,我是使用旧编译器堆栈的。而问问题的示例我是在另一个编译器中进行的。
1>c:\programming\boost_1_61_0\boost\proto\detail\poly_function.hpp(205): error C2039: 'type' : is not a member of 'boost::proto::functional::at::result<Sig>'
1>          with
1>          [
1>              Sig=boost::proto::functional::at (boost::phoenix::vector2<const boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::phoenix::detail::tag::function_eval,boost::proto::argsns_::list3<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<boost::phoenix::impl::for_each>,0>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<boost::reference_wrapper<const std::vector<int,std::allocator<int>>>>,0>>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::shift_left,boost::proto::argsns_::list2<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::shift_left,boost::proto::argsns_::list2<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::shift_left,boost::proto::argsns_::list2<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::shift_left,boost::proto::argsns_::list2<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::shift_left,boost::proto::argsns_::list2<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<std::basic_ostream<char,std::char_traits<char>> &>,0>>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<char>,0>>>,2>>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<boost::phoenix::argument<1>>,0>>>,2>>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<char [3]>,0>>>,2>>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<boost::phoenix::argument<2>>,0>>>,2>>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<char [4]>,0>>>,2>>>,3>> *,const int &> &,boost::mpl::int_<2>)
1>          ]
…
- for_each(begin,end,outer_expr);
- outer_expr=for_each(begin,end,inner_expr);
std::for_each(small_ints.cbegin(), small_ints.cend(),
            phx::for_each(phx::cref(big_ints),
            phx::lambda(_a=arg1)[phx::ref(std::cout) << '(' << _a << ", " << arg1 << "), "]
        )
    );
boost::phoenix::placeholders::arg1_type current_small_int, current_big_int;
boost::phoenix::local_names::_a_type passed_small_int;

std::for_each(small_ints.cbegin(), small_ints.cend(),
            phx::for_each(phx::cref(big_ints),
            phx::lambda(passed_small_int=current_small_int)[phx::ref(std::cout) << '(' << passed_small_int << ", " << current_big_int << "), "]
        )
    );
#include <iostream>
#include <algorithm>
#include <vector>
#include <boost/phoenix.hpp>


int main()
{
    std::vector<int> const small_ints {1, 2, 3, 4, 5};
    std::vector<int> const big_ints {11, 12, 13, 14, 15};

    namespace phx = boost::phoenix;
    using boost::phoenix::placeholders::arg1;
    using boost::phoenix::local_names::_a;

    std::vector<int>::const_iterator big_ints_it;
    std::for_each(small_ints.cbegin(), small_ints.cend(),
        phx::for_each(phx::cref(big_ints),
        phx::lambda(_a=arg1)[phx::ref(std::cout) << '(' << _a << ", " << arg1 << "), "]
    )
    );
}