473,320 Members | 2,164 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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::string n)
{
name = n;
}
};

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

main()
{
Foo foo;

SetterFunc f = std::bind1st(mem_fun(&Foo::setName), &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(mem_func(&Foo::getName), &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::string" ? Do I need mem_fun_ref or some const variation of it?

Thanks for any tips.

Sep 18 '07 #1
8 6325
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::string n)
{
name = n;
}
};

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

main()
{
Foo foo;

SetterFunc f = std::bind1st(mem_fun(&Foo::setName), &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(mem_func(&Foo::getName), &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::string" ? 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<iostream>
#include<functional>
#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(std::mem_fun(&Foo::getName),
&foo);

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

$ g++ tmp.cc
error: no type named `second_argument_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********@hotmail.comwrote:
class Foo
{
std::string name;
public:
Foo() : name("mike") {}

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

void setName(std::string n)
{
name = n;
}
};
typedef std::binder1st<std::mem_fun_t<std::string, Foo GetterFunc;
GetterFunc y = std::bind1st(mem_func(&Foo::getName), &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::string" ? 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<iostream>
#include<functional>
#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(std::mem_fun(&Foo::getName),
&foo);

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

$ g++ tmp.cc
error: no type named `second_argument_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::getName)
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.********@comAcast.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_wrapper<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::getName, &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
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>...
2
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)); ...
9
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
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
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. ...
4
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
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <string> #include <list> #include <algorithm> using namespace std; class Test
1
by: subramanian100in | last post by:
Suppose I have class WordAndLineNumbers { public: void print( ); // other member functions private: // data members
1
by: jonnyothan | last post by:
The following code isn't producing expected results: class Selectable { bool IsSelected() const; }; class Unit : public Selectable { };
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.