boostorg / range

Boost.org range module
http://boost.org/libs/range
43 stars 104 forks source link

Dedicated adaptor fetching data from an object member #115

Open trueqbit opened 3 years ago

trueqbit commented 3 years ago

In order to make code even more easily readable, I started encapsulating the creation of a "transform" range adaptor that accesses a member variable:

inline constexpr auto fetch = [](auto memberPtr)
{
    return boost::adaptors::transformed(std::bind(memberPtr, std::placeholders::_1));
};

// example
sort(c | fetch(&Object::name));

This solves the boiler-plate code required otherwise:

sort(c | transformed(std::bind(&Object::name, std::placeholders::_1)));

Do you think this is a generic enough pattern worthwhile to be added to "boost range"?