|
Boost : |
From: jkharris01 (john.harris_at_[hidden])
Date: 2002-02-20 18:15:57
Should bind() be extended to include binding to member data? If it
already allows convenient binding to member functions, wouldn't
member data be a natural extension?
Perhaps the operator() of the class resulting from such a bind would
return a reference to the member data.
I've run this by the author of boost::function, Douglas Gregor
(privately), and he thinks it would be a useful addition.
The specific problem that got me thinking about this is that I
couldn't find an easy way to make a predicate compare only
the ".second" member of a pair, as in a map lookup by value:
typedef std::map<int,float> map_t;
// find the map entry whose value is 3.14
void findpi(const map_t& themap)
{
std::find_if(themap.begin(), themap.end(),
member_equal(&map_t::value_type::second, 3.14));
}
My solution to this, by the way, goes like this:
template<typename R, typename T>
class MemData_t
{
public:
MemData_t(R T::* pmd_)
: pmd(pmd_)
{
}
const R& operator()(const T& inst_)
{
return inst_.*pmd;
}
private:
R T::* pmd; // ptr to member data
};
template <typename T, typename R> boost::function<bool,T>
member_equal(R T::* pmd_, R stator_)
{
return boost::bind<bool>(
std::equal_to<R>(),
boost::bind<const R&>(MemData_t<R,T>(pmd_),_1),
stator_);
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk