By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

on std::bind2nd

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
Thank you.
I've got it fixed using Boost.
Happy to learn something today.

--
ES Kim
Jul 22 '05 #3

P: n/a
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.