How to use boost.lambda with boost.range to select from container?

In C # with Linq, I would write:

myContainer.Select(o => o.myMember); 

I'm not sure what the syntax should be for C ++ / lambda / range. I'm just trying to adapt a container of some type of object to a string container so that I can pass it to boost :: algorithm :: join. I tried something like:

 using namespace boost::adaptors; using namespace boost::lambda; string result = join(myContainer | transformed(_1.myMember), ", "); 

But obviously this does not work, or I would not be here.;)

I use converted after reading: http://www.boost.org/doc/libs/1_43_0/libs/range/doc/html/range/reference/adaptors/reference/transformed.html

I want to use lambda instead of a separate function for brevity.

+4
source share
1 answer

operator. is not overloaded, so it can never do anything reasonable on a placeholder.


Boost.Lambda (and Boost.Phoenix v1 and v2, which were based on Boost.Lambda) implements its own result protocol, not TR1, so Boost.Lambda functions will not work with anything using boost::result_of or std::tr1::result_of (as Boost.Range does).

However, Boost.Phoenix v3, released in Boost 1.47, is the official replacement for Boost.Lambda and implements the TR1 protocol result_of, and therefore plays well with boost::result_of (and therefore Boost.Range).

Your options should either use Boost.Bind instead of Boost.Lambda, in which case the following is valid:

 transformed(bind(&myObjectType::myMember, _1)) 

or you can use Boost.Phoenix v3 instead of Boost.Lambda (either get Boost.Phoenix from the trunk now, or wait for Boost 1.47), in which case the Boost.Bind syntax is valid, as well as the following option

 transformed(_1->*&myObjectType::myMember) 
+3
source

All Articles