std::mem_fun_ref and bind2nd question

D

dzikus

Why the following code does not compile? If I change the function
argument to value (not reference) everything is ok. Is it possible to
modify such code that it will compile with reference argument type?

Best regards
Dominik

#include <string>
#include <list>
#include <algorithm>

struct Data {
void f(const std::string& str) { }
};

void main() {
std::list<Data> l;
std::for_each(l.begin(), l.end(),
std::bind2nd(std::mem_fun_ref(&Data::f), "Test"));
}

And error message:
see reference to class template instantiation 'std::binder2nd<_Fn2>'
being compiled
with
[
_Fn2=std::mem_fun1_ref_t<void,Data,const std::string &>
]
error C2529: '_Right' : reference to reference is illegal
 
M

Mark Stijnman

dzikus said:
Why the following code does not compile? If I change the function
argument to value (not reference) everything is ok. Is it possible to
modify such code that it will compile with reference argument type?

Best regards
Dominik

#include <string>
#include <list>
#include <algorithm>

struct Data {
void f(const std::string& str) { }
};

void main() {
std::list<Data> l;
std::for_each(l.begin(), l.end(),
std::bind2nd(std::mem_fun_ref(&Data::f), "Test"));
}

And error message:
see reference to class template instantiation 'std::binder2nd<_Fn2>'
being compiled
with
[
_Fn2=std::mem_fun1_ref_t<void,Data,const std::string &>
]
error C2529: '_Right' : reference to reference is illegal

bind2nd works on functions taking two arguments, setting the right
argument to a constant value ("Test" in your case). Your 'Data::f' only
has one argument.
 
T

Thomas Maier-Komor

dzikus said:
Why the following code does not compile? If I change the function
argument to value (not reference) everything is ok. Is it possible to
modify such code that it will compile with reference argument type?

Best regards
Dominik

#include <string>
#include <list>
#include <algorithm>

struct Data {
void f(const std::string& str) { }
};

void main() {
std::list<Data> l;
std::for_each(l.begin(), l.end(),
std::bind2nd(std::mem_fun_ref(&Data::f), "Test"));
}

And error message:
see reference to class template instantiation 'std::binder2nd<_Fn2>'
being compiled
with
[
_Fn2=std::mem_fun1_ref_t<void,Data,const std::string &>
]
error C2529: '_Right' : reference to reference is illegal

mem_fun_ref and mum_fun require a constant member function as an
argument. Furthermore the parameter of the member function must
not have reference type. So you will have to write a functor:

e.g.

struct Data {
void f(const std::string &str) {}
};

struct F {
void operator () (Data &d) { d.f(s); }
F (const std::string &str)
: s(str)
{ }
private:
const std::string &s;
};

int main() {
std::list<Data> l;
std::for_each(l.begin(), l.end(), F("test"));
return 0;
}
 
T

Thomas Maier-Komor

Mark said:
bind2nd works on functions taking two arguments, setting the right
argument to a constant value ("Test" in your case). Your 'Data::f' only
has one argument.

no it has two arguments: the implicite this and str.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top