473,761 Members | 9,864 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

mem_fun fun

Hi,

I am having some problem using std::bind1st() and mem_fun. I want to
bind member function calls to some kind of functor so it can be called
later.

The following works fine for me:

class Foo
{
std::string name;
public:
Foo() : name("mike") {}

std::string getName()
{
return name;
}

void setName(std::st ring n)
{
name = n;
}
};

typedef std::binder1st< std::mem_fun1_t <void, Foo, std::string
SetterFunc;

main()
{
Foo foo;

SetterFunc f = std::bind1st(me m_fun(&Foo::set Name), &foo);

std::string test("testing") ;
f(test);

std::cout << "after call, name is " << foo.getName() <<
std::endl;
}
But when I want to bind the getName() method, I have lots of trouble:

typedef std::binder1st< std::mem_fun_t< std::string, Foo GetterFunc;
GetterFunc y = std::bind1st(me m_func(&Foo::ge tName), &foo);

I think I don't want to use bind1st because the getName() method takes
no parameters? Is there a std::bind() ?

Also, what if setName() takes "const std::string&" instead of just
"std::strin g" ? Do I need mem_fun_ref or some const variation of it?

Thanks for any tips.

Sep 18 '07 #1
8 6349
flopbucket wrote:
I am having some problem using std::bind1st() and mem_fun. I want to
bind member function calls to some kind of functor so it can be called
later.

The following works fine for me:

class Foo
{
std::string name;
public:
Foo() : name("mike") {}

std::string getName()
{
return name;
}

void setName(std::st ring n)
{
name = n;
}
};

typedef std::binder1st< std::mem_fun1_t <void, Foo, std::string
SetterFunc;

main()
{
Foo foo;

SetterFunc f = std::bind1st(me m_fun(&Foo::set Name), &foo);

std::string test("testing") ;
f(test);

std::cout << "after call, name is " << foo.getName() <<
std::endl;
}
But when I want to bind the getName() method, I have lots of trouble:

typedef std::binder1st< std::mem_fun_t< std::string, Foo GetterFunc;
GetterFunc y = std::bind1st(me m_func(&Foo::ge tName), &foo);
What kind of trouble? Why couldn't you post non-working code instead?
I think I don't want to use bind1st because the getName() method takes
no parameters? Is there a std::bind() ?
Why wouldn't you? 'bind1st' makes sure that the member function is
called with the particular instance, 'foo'.
>
Also, what if setName() takes "const std::string&" instead of just
"std::strin g" ? Do I need mem_fun_ref or some const variation of it?
Probably. Have tried it an failed or what?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 18 '07 #2
What kind of trouble? Why couldn't you post non-working code instead?
>
Thanks for your response, here is a non-working example:

#include<iostre am>
#include<functi onal>
#include<string >

class Foo
{
std::string name_;

public:
Foo() : name_("mike")
{}

std::string getName()
{
return name_;
}
};

typedef std::binder1st< std::mem_fun_t< std::string, Foo GetterFunc;

int main(int, char **)
{
Foo foo();

GetterFunc f = std::bind1st(st d::mem_fun(&Foo ::getName),
&foo);

std::cout << f() << std::endl;
}

$ g++ tmp.cc
error: no type named `second_argumen t_type' in `class
std::mem_fun_t< std::string, Foo>'
Basically I would like to place the call foo->getName() into a functor
so I could call it later.

Thanks for any tips.
Sep 18 '07 #3
flopbucket <fl********@hot mail.comwrote:
class Foo
{
std::string name;
public:
Foo() : name("mike") {}

std::string getName()
{
return name;
}

void setName(std::st ring n)
{
name = n;
}
};
typedef std::binder1st< std::mem_fun_t< std::string, Foo GetterFunc;
GetterFunc y = std::bind1st(me m_func(&Foo::ge tName), &foo);

I think I don't want to use bind1st because the getName() method takes
no parameters? Is there a std::bind() ?
Correct to the first question, and no to the second. You will have to
make your own binder for this.
Also, what if setName() takes "const std::string&" instead of just
"std::strin g" ? Do I need mem_fun_ref or some const variation of it?
If setName takes a const string& then you can't use bind1st because you
can't make a reference to a reference. I think that is supposed to be
fixed in the next standard isn't it?
Sep 18 '07 #4
flopbucket wrote:
>What kind of trouble? Why couldn't you post non-working code
instead?

Thanks for your response, here is a non-working example:

#include<iostre am>
#include<functi onal>
#include<string >

class Foo
{
std::string name_;

public:
Foo() : name_("mike")
{}

std::string getName()
{
return name_;
}
};

typedef std::binder1st< std::mem_fun_t< std::string, Foo GetterFunc;

int main(int, char **)
{
Foo foo();

GetterFunc f = std::bind1st(st d::mem_fun(&Foo ::getName),
&foo);

std::cout << f() << std::endl;
}

$ g++ tmp.cc
error: no type named `second_argumen t_type' in `class
std::mem_fun_t< std::string, Foo>'
Basically I would like to place the call foo->getName() into a functor
so I could call it later.
Right. Well, the problem is that 'bind1st' (and 'binder1st') need their
argument to be a function of two arguments. Since mem_fun(&Foo::g etName)
returns a functor with only one argument, it cannot be use in 'bind1st'.

You probably should consider using 'bind' which overcomes the limitation
of "exactly two arguments" imposed by 'binder1st'. Or you could add
a dummy argument to 'getName', thus making it a function with a second
argument, sort of. A hassle, admittedly.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 18 '07 #5
>
You probably should consider using 'bind' which overcomes the limitation
of "exactly two arguments" imposed by 'binder1st'.

V

Thanks to all for the responses. Is there a std::bind()? Or is that
part of Boost? I'm not finding a std::bind() in my environment. I
guess it's pretty easy to write a simple wrapper for this, but I would
rather use something standard and existing then rolling my own.

Thanks again.
Sep 18 '07 #6
On 2007-09-18 15:38:57 -0400, "Victor Bazarov" <v.********@com Acast.netsaid:
flopbucket wrote:
>I think I don't want to use bind1st because the getName() method takes
no parameters? Is there a std::bind() ?

Why wouldn't you? 'bind1st' makes sure that the member function is
called with the particular instance, 'foo'.
No, that's the problem: bind1st converts a binary function into a unary
function by binding the first argument of the binary function to the
argument passed to bind1st. The resulting object has a function call
operator that takes one argument, and passes it as the second argument
to the binary function. (Whew. That's confusing.)

There is an std::bind() that will do what's needed, but it's not the
current standard (it's in TR1 and slated for C++0x). Calling
std::bind(&Foo: :getName, &foo) returns an object whose operator() [with
no arguments] returns (&foo)->getName(). It also works with that second
argument passed by value, in which case it uses a copy, with that
second argument passed as a reference_wrapp er<Foo>, in which case it
uses a referece to foo, and with that second argument passed by a smart
pointer (anything that provides an operator-that returns a pointer to
something that getName() can be applied to). For more details, see
chapter 10 of my book, "The Standard C++ Library Extensions".

The drawback here, for the original poster, is that the return type of
std::bind() is unspecified, so it's hard to create named objects of
this type other than as arguments and local variables in template
functions. The following code was written but not compiled:

template <class Op>
std::string apply(Op op)
{
return op();
}

int main()
{
Foo foo;
std::string res = apply(std::bind (&Foo::getNam e, &foo));
return 0;
}

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 18 '07 #7
flopbucket wrote:
>You probably should consider using 'bind' which overcomes the
limitation of "exactly two arguments" imposed by 'binder1st'.

V


Thanks to all for the responses. Is there a std::bind()?
If your compiler doesn't have it yet, it will, eventually. It's in the
currently proposed draft. I am guessing we should see it adopted next
year or the year after.
Or is that
part of Boost?
Yes.
I'm not finding a std::bind() in my environment. I
guess it's pretty easy to write a simple wrapper for this, but I would
rather use something standard and existing then rolling my own.
Good idea.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 18 '07 #8


Thanks again for all the replies, very much appreciated.
Sep 19 '07 #9

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

Similar topics

5
4932
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)
2
1996
by: Robbie Hatley | last post by:
I've got a function that I use a lot when making utility programs that need to do the same thing to every directory in a tree. Its prototype is: unsigned long int CursDirs (void Func(void)); This just applies the fuction Func to every subdirectory of the current directory. It works fine when I pass it pointers to regular void-void functions.
9
2818
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
5928
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
3115
by: ShaneG | last post by:
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...
4
2603
by: joseph cook | last post by:
I am getting a compile error on any compiler I try, so I know I have an error here. Can anyone see it? //includes class Foo { public: Foo(int a){m_hi = a;} int hi(){return m_hi;}
1
1636
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <string> #include <list> #include <algorithm> using namespace std; class Test
1
1428
by: subramanian100in | last post by:
Suppose I have class WordAndLineNumbers { public: void print( ); // other member functions private: // data members
1
1814
by: jonnyothan | last post by:
The following code isn't producing expected results: class Selectable { bool IsSelected() const; }; class Unit : public Selectable { };
0
9531
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10115
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7332
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6609
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3881
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
3
3456
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.