469,140 Members | 1,248 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,140 developers. It's quick & easy.

on std::bind2nd

Here's a simple code:

#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

struct S
{
void f(int) const { }
void g(const int&) const { }
};

int main()
{
int n = 1;
vector<S> v;
for_each(v.begin(), v.end(), bind2nd(mem_fun_ref(&S::f), n));
for_each(v.begin(), v.end(), bind2nd(mem_fun_ref(&S::g), n));
}

What is the reason why the second for_each() doesn't compile?

--
ES Kim
Jul 22 '05 #1
3 2500
On Wed, 2 Jun 2004 10:52:27 +0900, "ES Kim" <no@spam.mail> wrote:
Here's a simple code:

#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

struct S
{
void f(int) const { }
void g(const int&) const { }
};

int main()
{
int n = 1;
vector<S> v;
for_each(v.begin(), v.end(), bind2nd(mem_fun_ref(&S::f), n));
for_each(v.begin(), v.end(), bind2nd(mem_fun_ref(&S::g), n));
}

What is the reason why the second for_each() doesn't compile?


This is the "reference-to-reference" problem; Scott Meyers discusses it in
Item #50 of _Effective STL_, where he mentions that the Boost library
provides alternate function objects to get around the problem. I haven't
played with them, but you may get lucky at www.boost.org.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Thank you.
I've got it fixed using Boost.
Happy to learn something today.

--
ES Kim
Jul 22 '05 #3
On Wed, 2 Jun 2004 12:44:28 +0900, "ES Kim" <no@spam.mail> wrote:
Thank you.
I've got it fixed using Boost.
Happy to learn something today.


Great! And I just realized I wrote "function objects" when I actually
meant "adapters", but that doesn't seem to have slowed you down. Good
job!
-leor
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by red floyd | last post: by
3 posts views Thread by John Black | last post: by
4 posts views Thread by Steven T. Hatton | last post: by
4 posts views Thread by dzikus | last post: by
6 posts views Thread by ma740988 | last post: by
8 posts views Thread by Noah Roberts | last post: by
2 posts views Thread by benben | last post: by
3 posts views Thread by Bruintje Beer | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.