Search code examples
c++boostboost-phoenix

How to use range-based for_each in Boost.Phoenix?


Let's consider following code:

#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) << "), "
        ]
    );
}

It works fine by printing expected sequence of pairs

(1,11), (1,12), (1,13), (1,14), (1,15), (2,11), (2,12), …, (5,14), (5,15),

which is just Cartesian product resulting from double iteration.

However the for_ is ugly! Look at all those ref, cref (that somehow I always have to explicitly select from phoenix as otherwise there are conflicts with std versions). Not to mention that I have to keep useless big_ints_it variable!

Note also that this follows example provided by Phoenix documentation itself.

Now I tried to use for_each mentioned along iteration algorithms, expecting much simpler version:

std::for_each(small_ints.cbegin(), small_ints.cend(),
    for_each(phoenix::cref(big_ints),
        std::cout << val('(') << arg1 << ", " << arg2 << "), "
    )
);

but it doesn't even compile! Or at least with Visual Studio 2013 and Boost 1.61.

I'm getting multiple errors. First of which is

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>          ]
…

I cannot even enter the whole first error since StackOverflow says, that question body is limited to 30,000 characters which I have entered 135,034 of them…


Solution

  • The problem is your use of arg2 in your inner expression. Let's have a look at the situation.

    You basically have:

    - for_each(begin,end,outer_expr);
    - outer_expr=for_each(begin,end,inner_expr);
    

    Both outer_expr and inner_expr are lambda expression that take a single argument (an element of its respective vectors).

    What you need is a way to pass the argument from outer_expr to inner_expr. Luckily Boost.Phoenix provides an easy way to do just that: boost::phoenix::lambda:

    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 << "), "]
            )
        );
    

    Or getting a little silly, but maybe helping readability:

    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 << "), "]
            )
        );
    

    Full Sample (Running on WandBox)

    #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 << "), "]
        )
        );
    }
    

    PS: If your compiler can initialize the vectors like that, it should probably be able to use actual lambdas and that would probably result in less headaches.