What's the boost::phoenix equivalent of this?

I have the following:

class Foo
{
public:
   std::string const& Value() const { return /*Return some string*/; }
};

typedef std::list<Foo> FooList;
FooList foos; // Assume it has some valid entities inside

std::vector<int> ints;

FooList::const_iterator it, iend = foos.end();
for (it = foos.begin(); it != iend; ++it)
{
   ints.push_back(boost::lexical_cast<int>(it->Value()));
}

How can I implement the for loop using std::for_each and boost::phoenix? I tried a few approaches but it gets really really ugly (I had a ton of nested bind() statements). I basically just want to see how clean & readable boost phoenix can make this for loop, so I'm not writing so much boilerplate code to iterate containers that have 1-2 lines of specialized logic.

Sometimes, doing lambdas pre-C++11 just seems too unreadable and unmaintainable to be worth the trouble.

Answers


Assuming you prepare a Phoenix-friendly function object:

namespace lexical_casts
{
    template <typename T> struct to_
    {
        template <typename/*V*/> struct result { typedef T type; };
        template <typename V>
        T operator()(V const& v) const { return boost::lexical_cast<T>(v); }
    };

    boost::phoenix::function<to_<int> > to_int;
}

You can write things like:

BOOST_AUTO(value_of, phx::lambda[ phx::bind(&Foo::Value, arg1) ]);

std::vector<int> ints;
boost::transform(
        foolist,
        back_inserter(ints), 
        lexical_casts::to_int(value_of(arg1)));

See it Live On Coliru


Need Your Help

Telerik - Rad Combob Box - Jquery - OnSelectedIndexChange

c# asp.net jquery web-applications telerik

how can i fire telerik rad combo box selectedindexchange event With Jquery...