boost::mem_fn erases the need for a separate mem_fn_ref. If
http://www.boost.org/libs/bind/mem_fn.html fails to explain it to you,
please ask more specific questions.
I thought I'd been pretty specific... guess not. Allow me to reiterate:
Why doesn't std::mem_fun work with a container of boost::shared_ptr?
I fiddled around with my compiler for a while (vc6), but ended up with a wildly unhelpful error message. Given the following lines of code (among others):
---
typedef std::vectorstd::string > Strings;
Strings strings;
std::for_each( strings.begin(), strings.end(), std::mem_fun(std::string::size) );
---
my compiler puked up the following wildly useless error message:
---
error C2664: 'mem_fun' : cannot convert parameter 1 from 'unsigned int (__thiscall std::basic_string::*' to 'unsigned int (__thisca
ll std::basic_string::*'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
---
Allow me to pull out those two types for easier viewing:
unsigned int (__thiscall std::basic_string::*
unsigned int (__thiscall std::basic_string::*
Golly. Those 'two' types are about as related as you can get.
So why won't std::mem_fun work through boost::shared_ptr's operator->?
Here's the mem_fun that ships with VC6:
---
template
class mem_fun_t : public unary_function<_Ty *, _R> {
public:
explicit mem_fun_t(_R (_Ty::*_Pm)())
: _Ptr(_Pm) {}
_R operator()(_Ty *_P) const
{return ((_P->*_Ptr)()); }
private:
_R (_Ty::*_Ptr)();
};
---
The relevant portion to our discussion is: "{return ((_P->*_Ptr()); }" which can be further boiled down to:
return _P->_Ptr();
This looks an awful lot like it should work with a shared_ptr's operator->(). So why doesn't it?
I admit that this is more of a C++ question than a boost one, but it's not too much of a tangent and I *am* curious.
Mentioning mem_fun_ref in my previous post only served to muddy the waters. My apologies.
--Mark Storer
Senior Software Engineer
Verity, Inc.