473,480 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

on std::bind2nd

Here's a simple code:

#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

struct S
{
void f(int) const { }
void g(const int&) const { }
};

int main()
{
int n = 1;
vector<S> v;
for_each(v.begin(), v.end(), bind2nd(mem_fun_ref(&S::f), n));
for_each(v.begin(), v.end(), bind2nd(mem_fun_ref(&S::g), n));
}

What is the reason why the second for_each() doesn't compile?

--
ES Kim
Jul 22 '05 #1
3 2654
On Wed, 2 Jun 2004 10:52:27 +0900, "ES Kim" <no@spam.mail> wrote:
Here's a simple code:

#include <vector>
#include <algorithm>
#include <functional>
using namespace std;

struct S
{
void f(int) const { }
void g(const int&) const { }
};

int main()
{
int n = 1;
vector<S> v;
for_each(v.begin(), v.end(), bind2nd(mem_fun_ref(&S::f), n));
for_each(v.begin(), v.end(), bind2nd(mem_fun_ref(&S::g), n));
}

What is the reason why the second for_each() doesn't compile?


This is the "reference-to-reference" problem; Scott Meyers discusses it in
Item #50 of _Effective STL_, where he mentions that the Boost library
provides alternate function objects to get around the problem. I haven't
played with them, but you may get lucky at www.boost.org.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #2
Thank you.
I've got it fixed using Boost.
Happy to learn something today.

--
ES Kim
Jul 22 '05 #3
On Wed, 2 Jun 2004 12:44:28 +0900, "ES Kim" <no@spam.mail> wrote:
Thank you.
I've got it fixed using Boost.
Happy to learn something today.


Great! And I just realized I wrote "function objects" when I actually
meant "adapters", but that doesn't seem to have slowed you down. Good
job!
-leor
Jul 22 '05 #4

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

Similar topics

1
3362
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
1922
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...
4
3642
by: Steven T. Hatton | last post by:
This code works for dividing each element of a boost::array<> by a value of its element type: template <typename T, size_t S> inline boost::array<T, S>& operator/=( boost::array<T, S>& lhs,...
4
4885
by: dzikus | last post by:
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...
6
3222
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...
8
4484
by: Noah Roberts | last post by:
#include <vector> #include <algorithm> #include <functional> class X { int x; public: X(int i) : x(i) {} bool eq(const X & other) const { return x == other.x; }
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
1797
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
6151
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
7039
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
7080
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...
1
6735
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...
0
6895
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
5326
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,...
0
4476
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...
0
1296
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
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
176
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...

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.