472,338 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

std::mem_fun_ref and bind2nd question

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
Jul 23 '05 #1
4 4830
dzikus wrote:
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.

Jul 23 '05 #2
dzikus wrote:
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;
}
Jul 23 '05 #3
Mark Stijnman schrieb:
dzikus wrote:
struct Data {
void f(const std::string& str) { }
};

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.
Jul 23 '05 #4

Thomas Maier-Komor wrote:
no it has two arguments: the implicite this and str.


ah, right, my bad :)

Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: red floyd | last post by:
Both gcc 3.3.1 and MSVC 7.1 complain about the second for_each() call in the following code. Why can't I use bind2nd for a functor with a non-const...
3
by: John Black | last post by:
Hi, I have some code like this, itr = find_if(this->pool.begin(), this->pool.end(), bind2nd(mem_fun_ref(&Pool::isAvailable), make_pair(base,...
3
by: ES Kim | last post by:
Here's a simple code: #include <vector> #include <algorithm> #include <functional> using namespace std; struct S { void f(int) const { }
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? --...
5
by: sparks | last post by:
We have a person that has 4 columns and 20 rows of data. when they have all the info they want to put in the data a row at a time. BUT if they...
6
by: ma740988 | last post by:
I've been perusing for some time now - I suppose some of the algorithms in Josuttis. 'Today', I do alot of funny math on data resident within...
2
by: benben | last post by:
I wrote the following lines: #include <locale> #include <functional> #include <algorithm> int main() { using namespace std;...
3
by: Bruintje Beer | last post by:
Hi, I am having the following question (see code below) the class Data is declared as class Data { public : // rest of class };
4
by: Giovanni Gherdovich | last post by:
Hello, I'm doing some toy experiments to see how the algoritm std::transform and the function adapter std::bind2nd can play together, but my...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.