Boost logo

Boost :

Subject: Re: [boost] [range] Adapter to select class member
From: Robert Jones (robertgbjones_at_[hidden])
Date: 2012-12-17 11:38:40


On Mon, Dec 17, 2012 at 4:29 PM, Mark Snelling <mark_at_[hidden]> wrote:

> Hi,
> I'm not sure if this has already been done but I have created a new
> Boost.Range adapter that will return a specified member variable of the
> class in the range, similar to the existing map_keys and map_values
> adapters.
> Would there be interest in this?
>
> For example consider the following code:
>
> #include <boost/range/adaptor/member.hpp>
> #include <boost/range/algorithm/copy.hpp>
> #include <boost/lexical_cast.hpp>
> #include <iostream>
> #include <vector>
> #include <string>
>
> struct A
> {
> int foo;
> std::string bar;
> };
>
> int main(int argc, char* argv[])
> {
> std::vector< A > input;
> for ( int i = 0; i < 10; ++i )
> {
> A a;
> a.foo = i;
> a.bar = boost::lexical_cast< std::string >( i * 10 );
> input.push_back( a );
> }
>
> boost::copy(
> input | boost::adaptors::selected_member( &A::foo ),
> std::ostream_iterator< int >( std::cout, "," ) );
>
> std::cout << std::endl;
>
> boost::copy(
> input | boost::adaptors::selected_member( &A::bar ),
> std::ostream_iterator< std::string >( std::cout, "," ) );
>
> return 0;
> }
>
> Cheers,
>
> Mark
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>

How is this different to

input | boost::adaptors::transformed( bind( &A::bar ) ) ?

I guess your solution avoids the explicit bind, but if that's an often used
shorthand I'd prefer
to see a more general solution for transformed, filtered and maybe others
that interprets a method
or member address in this way when passed as a functor or predicate.

- Rob.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk