Boost logo

Boost :

From: John Torjo (john.lists_at_[hidden])
Date: 2004-02-26 13:27:00


Fredrik wrote:

>John Torjo wrote:
>
>
>>Hi all,
>>
>>
>
>
>
>>I think since member functions can be wrapped up as >functors, why not
>>same for member data?
>>Therefore, I think this could be a useful addition to >functional:
>>
>>
><<snip>>
>
>
>>What do you think?
>>
>>
>
>I think Peter Dimov has already thought about this ;) Read the current mem_fn docs carefully and you'll find a data-case also.
>I agree it's very useful functionality. It was added fairly "silently" though (mid 2002 IIRC) which might have made people miss it(?).
>
>
>

Indeed! Quite cool!

What I miss is the fact that I want mem_fn to treat the object
non-const, and have one more function (lets call it const_mem_fn) which
would treat the object as non-const.
At this time, mem_fn treats the object as const:
// mem_fn.hpp, line 256
    typedef R const & result_type;
    typedef T const * argument_type;

If we were to have what I said above, we could simply modify any
sequence inplace. Combining it with bind, and the possibilities are
limit-less.

Imagine this:

struct test {
   int i;
   std::string s;
};
std::vector<test> tests;
// ... fill

void set(int & i, int val) { i = val };
int val = 10;
// for each obj from vector, set obj.i = val;
std::for_each( tests.begin(), tests.end(), bind(&decrement,
bind(mem_fn(&test::i),_1), val);

Changes to mem_fn are fairly easy. I could do them, if Peter is ok with it.
(basically, I still think it would be simpler to have one more mem_data
class to clearly diffenenciate from mem_fn, which always suggests a
member function)

Best,
John


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