473,724 Members | 2,246 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 4902
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
3378
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
1933
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
2668
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
5923
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
1325
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
3234
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
1814
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
6183
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
8741
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9388
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
9241
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
9160
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
9090
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8062
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...
1
3209
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
2612
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2149
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.