473,892 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::mem_fun_re f 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(st d::mem_fun_ref( &Data::f), "Test"));
}

And error message:
see reference to class template instantiation 'std::binder2nd <_Fn2>'
being compiled
with
[
_Fn2=std::mem_f un1_ref_t<void, Data,const std::string &>
]
error C2529: '_Right' : reference to reference is illegal
Jul 23 '05 #1
4 4916
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(st d::mem_fun_ref( &Data::f), "Test"));
}

And error message:
see reference to class template instantiation 'std::binder2nd <_Fn2>'
being compiled
with
[
_Fn2=std::mem_f un1_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(st d::mem_fun_ref( &Data::f), "Test"));
}

And error message:
see reference to class template instantiation 'std::binder2nd <_Fn2>'
being compiled
with
[
_Fn2=std::mem_f un1_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
3390
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 use kludge it by using something like KludgeFunctor(). --------------------------- #include <vector> #include <algorithm> #include <functional>
3
1939
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 template instantiation errors. My class definition is like this,
3
2680
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
5935
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
1331
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 enter that. with tab orders you can set it to go across or down each column. He wanted to know if he could use the down arrow to put in the one column. has anyone ever faced this or heard about someone wanting this, and how you
6
3245
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 std::search_n alot to find a particular value in a range of memory. I'm fairly pleased with std::search_n - in that my benchmark ( below ) shows on average an 8 second performance gain comparable to a for loop. The question. Since I haven't quite...
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
1827
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
6205
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 traslate' as `this' argument of `circle traslate::operator()(circle, std::vector<double, std::allocator<double)'
0
11241
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10836
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10926
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9644
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7172
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5857
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6065
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4683
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 we have to send another system
2
4279
muto222
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.