on std::bind2nd

E

ES Kim

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?
 
L

Leor Zolman

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
 
L

Leor Zolman

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top