473,486 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 4885
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
3363
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 reference as the second parameter? I have to...
3
1923
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, high))); But compiler always complains a lot STL...
3
2656
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
5897
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? -- Ioannis Vranos http://www23.brinkster.com/noicys
5
1317
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 only have the first column of data they want to...
6
3223
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 memory. As part of this. I find myself using...
2
483
by: benben | last post by:
I wrote the following lines: #include <locale> #include <functional> #include <algorithm> int main() { using namespace std; bind2nd(ptr_fun(tolower<char>), locale());
3
1800
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
6154
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 compiler give my the error error: passing `const...
0
7100
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7126
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4865
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3070
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.