473,566 Members | 2,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why do we need seperate ptr_fun/mem_fun/mem_fun_ref functions?


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.begi n(), c.end(), ptr_fun(test));
x = count_if(c.begi n(), c.end(), mem_fun_ref(&ob j::test));

vector<foo*> c2;
....
x = count_if(c2.beg in(), c2.end(), mem_fun(&obj::t est));

We could have:

x = count_if(c.begi n(), c.end(), fun(test));
x = count_if(c.begi n(), c.end(), fun(&obj::test) );
x = count_if(c2.beg in(), 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++.m oderated. First time posters: Do this! ]

Jul 23 '05 #1
4 3105
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/wg...al_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++.m oderated. First time posters: Do this! ]

Jul 23 '05 #2
On Mon, 23 May 2005 05:20:21 +0400, ShaneG <sh***@shaneand susan.com> 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.

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++.m oderated. First time posters: Do this! ]

Jul 23 '05 #3
"ShaneG" <sh***@shaneand susan.com> writes:
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++.m oderated. First time posters: Do this! ]

Jul 23 '05 #4
Alberto Barbati wrote:
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/wg...al_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++.m oderated. First time posters: Do this! ]

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3497
by: Anders | last post by:
Hello. If I was to call a method of class T, M for each element in a collection, I'd do it like this: vector<int> vec; .... for_each(vec.begin(), vec.end(), mem_fun_ref(&T::M)); Okay, I can see that mem_fun_REF makes it obvious that I must use a reference, but why? Is it not possible to pass a function by-reference? As I
7
2201
by: Rex_chaos | last post by:
Hi all, Here is a std::vector containing some std::complex<double>. I would like to take the amplitude of the complex array. i.e. v = amp of v = I try a template function but didn't work std::vector< complex<double> > v(5); std::vector< double > av(5);
5
4924
by: Old Wolf | last post by:
I have a member function that acts on an object. I would also like to have a member function that acts on a container of such objects, using std::for_each. I tried: #include <algorithm> #include <functional> struct bar { template<typename T> void foo(T const &); template<typename InIt> void foo(InIt begin, InIt end)
10
1428
by: Jason Heyes | last post by:
How do I rewrite the following code so that I am taking advantage of the STL? The purpose of the code is to determine whether a vector contains any objects of MyClass that are "true" (i.e., MyClass::is_true() returns true). bool found = false; vector<MyClass>::size_type i; for (i=0; i < objects.size(); i++) { MyClass object = objects; if...
9
2807
by: Mr X | last post by:
Can anyone tell how to fix the compile error for the program below? #include "iostream.h" #include <vector> #include <algorithm> #include <functional> #include <list> using namespace std;
13
5907
by: Ioannis Vranos | last post by:
What is the exact difference between mem_fun and mem_fun_ref, since in all examples I looked at, they are used in exactly the same way? -- Ioannis Vranos http://www23.brinkster.com/noicys
4
4895
by: dzikus | last post by:
Why the following code does not compile? If I change the function argument to value (not reference) everything is ok. Is it possible to modify such code that it will compile with reference argument type? Best regards Dominik #include <string> #include <list> #include <algorithm>
1
1414
by: subramanian100in | last post by:
Suppose I have class WordAndLineNumbers { public: void print( ); // other member functions private: // data members
13
2978
by: Soumen | last post by:
I wanted convert a mixed case string to a lower case one. And I tried following code: std::transform(mixedCaseString.begin(), mixedCaseString::end(), mixedCaseString.begin(), std::ptr_fun(tolower)); Even though I's including cctype and algorithm, I's getting compiler (g ++ 3.3.6) error: no matching function for call to...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.