|
Boost : |
From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-02-14 06:21:05
Fredrik Blomqvist wrote:
> What's the rationale for mem_fn to (in the case of data members) make
> it's return_type a reference?
It's because mem_fn returns a reference.
#include <boost/mem_fn.hpp>
#include <iostream>
template<class F, class A1> typename F::result_type call(F f, A1 a1)
{
return f(a1);
}
struct X
{
int i;
};
int main()
{
X x = { 5 };
boost::mem_fn(&X::i)(&x) = 42;
int const * p = &call(boost::mem_fn(&X::i), &x);
std::cout << *p << std::endl;
}
A data member is treated as if it was a member function that returns a
reference to the member. Exposing a const reference as result_type has its
limitations since mem_fn tries to preserve the (non-)constness of the data
member, but it's as close as it can get.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk