Boost logo

Boost Users :

From: Mark Storer (MStorer_at_[hidden])
Date: 2004-05-07 19:26:15


> 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::vector<boost::shared_ptr<std::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<char,struct std::char_traits<char>,class std::allocator<char> >::*' to 'unsigned int (__thisca
ll std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::*'
        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<char,struct std::char_traits<char>,class std::allocator<char> >::*
unsigned int (__thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::*
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 _R, class _Ty>
	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.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net