 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
ShaneG Guest
|
Posted: Mon May 23, 2005 1:20 am Post subject: Why do we need seperate ptr_fun/mem_fun/mem_fun_ref function |
|
|
We have ptr_fun to handle functions, mem_fun to handle member functions
that will be called through a pointer, and mem_fun_ref to handle member
functions that will be called through a reference.
First, why do we need to have seperate mem_fun/mem_fun_ref? The first
returns a mem_fun_t, and the second a mem_fun_ref_t. But the only
difference between mem_fun_t and mem_fun_ref_t is that they have
different operator() methods. But since the operator() methods have
different signatures, why not have a single object with an overloaded
operator() (one that takes pointers, and one that takes references)?
(call it a mem_fun_dual_t).
Assuming you could do that, we would just have ptr_fun and mem_fun.
But ptr_fun and mem_fun have different signatures, so again, we should
be able to have a single overloaded function that produces either a
mem_fun_daul_t or a ptr_fun_t based on which overload is called.
Then instead of:
vector<foo> c;
....
x = count_if(c.begin(), c.end(), ptr_fun(test));
x = count_if(c.begin(), c.end(), mem_fun_ref(&obj::test));
vector<foo*> c2;
....
x = count_if(c2.begin(), c2.end(), mem_fun(&obj::test));
We could have:
x = count_if(c.begin(), c.end(), fun(test));
x = count_if(c.begin(), c.end(), fun(&obj::test));
x = count_if(c2.begin(), c2.end(), fun(&obj::test));
The first case would know to create a ptr_fun_t based on overloading.
The second and third cases would both create a mem_fun_dual_t (based on
overloading), and the correct operator() would be called based on
overloading operator()(foo&) in the 2nd case, and operator()(foo*) in
the 3rd case).
I've tried implementing a simple version of this (for example, it
ignores all the different possible calling conventions on functions,
and const/non-const variations), and it seems to work, but I'm curious
why we have 3 seperate function adapters here, when it seems like we
could do with only one (well, there would still be two function
adapters, but the same overloaded function could be used to generate
the correct object for you).
I also just had a though that you could further overload 'fun' to
distinguish between functions that take foo* and foo& and then generate
a function object that would call the function correctly, regardless of
whether the container held foo or foo* (whereas with ptr_fun, 'test'
must take the same type that is held by the container), but I haven't
thought this through.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Mon May 23, 2005 11:44 am Post subject: Re: Why do we need seperate ptr_fun/mem_fun/mem_fun_ref func |
|
|
"ShaneG" <shane (AT) shaneandsusan (DOT) com> writes:
| Quote: | We have ptr_fun to handle functions, mem_fun to handle member functions
that will be called through a pointer, and mem_fun_ref to handle member
functions that will be called through a reference.
First, why do we need to have seperate mem_fun/mem_fun_ref?
|
We don't. See tr1/mem_fn or http://www.boost.org/libs/mem_fn.
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Mon May 23, 2005 11:47 am Post subject: Re: Why do we need seperate ptr_fun/mem_fun/mem_fun_ref func |
|
|
ShaneG wrote:
| Quote: | We have ptr_fun to handle functions, mem_fun to handle member functions
that will be called through a pointer, and mem_fun_ref to handle member
functions that will be called through a reference.
|
This is already going to be fixed in the next revision of the library,
(the so-called Technical Report 1 or TR1). Have a look here:
http://www.open-std.org/jtc1/sc22/wg21/docs/library_technical_report.html
and in particular, to paper N1455 "Enhanced Binders".
Alberto
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maxim Yegorushkin Guest
|
Posted: Mon May 23, 2005 11:48 am Post subject: Re: Why do we need seperate ptr_fun/mem_fun/mem_fun_ref func |
|
|
On Mon, 23 May 2005 05:20:21 +0400, ShaneG <shane (AT) shaneandsusan (DOT) com> wrote:
| Quote: | We have ptr_fun to handle functions, mem_fun to handle member functions
that will be called through a pointer, and mem_fun_ref to handle member
functions that will be called through a reference.
First, why do we need to have seperate mem_fun/mem_fun_ref?
|
Those of us who use boost::bind don't need them.
--
Maxim Yegorushkin
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alberto Barbati Guest
|
Posted: Tue May 24, 2005 10:29 am Post subject: Re: Why do we need seperate ptr_fun/mem_fun/mem_fun_ref func |
|
|
Alberto Barbati wrote:
| Quote: | ShaneG wrote:
We have ptr_fun to handle functions, mem_fun to handle member functions
that will be called through a pointer, and mem_fun_ref to handle member
functions that will be called through a reference.
This is already going to be fixed in the next revision of the library,
(the so-called Technical Report 1 or TR1). Have a look here:
http://www.open-std.org/jtc1/sc22/wg21/docs/library_technical_report.html
and in particular, to paper N1455 "Enhanced Binders".
|
Ops. Sorry. Clearly it was N1432 "Enhanced Member Pointer Adaptor"
Alberto
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|