473,499 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bind2nd, mem_fun, and references

#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; }
};

int main(int argc, char* argv[])
{
std::vector<Xxes(10);

X x(10);

std::vector<X>::iterator it = std::find_if(xes.begin(), xes.end(),
std::bind2nd(std::mem_fun(&X::eq), x));

return 0;
}

Compilation result:
1>c:\program files\microsoft visual studio 8\vc\include\functional(312)
: error C2529: '_Right' : reference to reference is illegal
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_argument_type& _Right)
: op(_Func), value(_Right)
{ // construct from functor and right operand
}

This would follow...

So, what is the correct and portable way to do what I want or is it
just not possible?

Jul 14 '06 #1
8 4485

Noah Roberts wrote:
So, what is the correct and portable way to do what I want or is it
just not possible?
Nevermind, looking in the standard at the interfaces to these objects
it is clear that it will never work. Need to create new binder2nd that
will.

Jul 14 '06 #2
* Noah Roberts:
#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; }
};

int main(int argc, char* argv[])
{
std::vector<Xxes(10);

X x(10);

std::vector<X>::iterator it = std::find_if(xes.begin(), xes.end(),
std::bind2nd(std::mem_fun(&X::eq), x));

return 0;
}

Compilation result:
1>c:\program files\microsoft visual studio 8\vc\include\functional(312)
: error C2529: '_Right' : reference to reference is illegal
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_argument_type& _Right)
: op(_Func), value(_Right)
{ // construct from functor and right operand
}

This would follow...

So, what is the correct and portable way to do what I want or is it
just not possible?
One way could be to use to Boost binders (don't know if that works, but
probably, else I would probably have remembered). Another way is to
define a free comparision function. A third, slight adjustment:

#include <vector>
#include <algorithm>
#include <functional>

class X
{
int x;
public:
X(int i) : x(i) {}
bool operator==(const X & other) const { return x == other.x; }
};

int main()
{
std::vector<Xxes(10, X(0));

X x(10);

std::vector<X>::iterator it = std::find_if(
xes.begin(),
xes.end(),
std::bind2nd(std::equal_to<X>(), x)
);
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 14 '06 #3

Alf P. Steinbach wrote:
* Noah Roberts:
#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; }
};

int main(int argc, char* argv[])
{
std::vector<Xxes(10);

X x(10);

std::vector<X>::iterator it = std::find_if(xes.begin(), xes.end(),
std::bind2nd(std::mem_fun(&X::eq), x));

return 0;
}

Compilation result:
1>c:\program files\microsoft visual studio 8\vc\include\functional(312)
: error C2529: '_Right' : reference to reference is illegal
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_argument_type& _Right)
: op(_Func), value(_Right)
{ // construct from functor and right operand
}

This would follow...

So, what is the correct and portable way to do what I want or is it
just not possible?

One way could be to use to Boost binders (don't know if that works, but
probably, else I would probably have remembered).
That's out until I can convince boss Boost good.
bool operator==(const X & other) const { return x == other.x; }
That would work except my real-world type is polymorphic.

Jul 14 '06 #4

Noah Roberts wrote:
Alf P. Steinbach wrote:
* Noah Roberts:
#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; }
};
>
int main(int argc, char* argv[])
{
std::vector<Xxes(10);
>
X x(10);
>
std::vector<X>::iterator it = std::find_if(xes.begin(), xes.end(),
std::bind2nd(std::mem_fun(&X::eq), x));
>
return 0;
}
>
Compilation result:
>
>
1>c:\program files\microsoft visual studio 8\vc\include\functional(312)
: error C2529: '_Right' : reference to reference is illegal
>
>
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_argument_type& _Right)
: op(_Func), value(_Right)
{ // construct from functor and right operand
}
>
This would follow...
>
So, what is the correct and portable way to do what I want or is it
just not possible?
One way could be to use to Boost binders (don't know if that works, but
probably, else I would probably have remembered).

That's out until I can convince boss Boost good.
bool operator==(const X & other) const { return x == other.x; }

That would work except my real-world type is polymorphic.
Thanks though.

Jul 14 '06 #5
"Noah Roberts" <ro**********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Alf P. Steinbach wrote:
>* Noah Roberts:
#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; }
};

int main(int argc, char* argv[])
{
std::vector<Xxes(10);

X x(10);

std::vector<X>::iterator it = std::find_if(xes.begin(), xes.end(),
std::bind2nd(std::mem_fun(&X::eq), x));

return 0;
}

Compilation result:
1>c:\program files\microsoft visual studio 8\vc\include\functional(312)
: error C2529: '_Right' : reference to reference is illegal
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_argument_type& _Right)
: op(_Func), value(_Right)
{ // construct from functor and right operand
}

This would follow...

So, what is the correct and portable way to do what I want or is it
just not possible?

One way could be to use to Boost binders (don't know if that works, but
probably, else I would probably have remembered).

That's out until I can convince boss Boost good.
Perhaps you can convince your boss that TR1 is good, since it will be
part of the next C++ Standard. And you can now get a version that works
out of the box with VC++ from us.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 15 '06 #6
Noah Roberts wrote:
>
Alf P. Steinbach wrote:
>* Noah Roberts:
#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; }
};

int main(int argc, char* argv[])
{
std::vector<Xxes(10);

X x(10);

std::vector<X>::iterator it = std::find_if(xes.begin(), xes.end(),
std::bind2nd(std::mem_fun(&X::eq), x));

return 0;
}

Compilation result:
1>c:\program files\microsoft visual studio 8\vc\include\functional(312)
: error C2529: '_Right' : reference to reference is illegal
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_argument_type& _Right)
: op(_Func), value(_Right)
{ // construct from functor and right operand
}

This would follow...

So, what is the correct and portable way to do what I want or is it
just not possible?

One way could be to use to Boost binders (don't know if that works, but
probably, else I would probably have remembered).

That's out until I can convince boss Boost good.
> bool operator==(const X & other) const { return x == other.x; }

That would work except my real-world type is polymorphic.
Well, not in your vector.

Jul 15 '06 #7
P.J. Plauger wrote:
"Noah Roberts" <ro**********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Alf P. Steinbach wrote:
* Noah Roberts:
#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; }
};

int main(int argc, char* argv[])
{
std::vector<Xxes(10);

X x(10);

std::vector<X>::iterator it = std::find_if(xes.begin(), xes.end(),
std::bind2nd(std::mem_fun(&X::eq), x));

return 0;
}

Compilation result:
1>c:\program files\microsoft visual studio 8\vc\include\functional(312)
: error C2529: '_Right' : reference to reference is illegal
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_argument_type& _Right)
: op(_Func), value(_Right)
{ // construct from functor and right operand
}

This would follow...

So, what is the correct and portable way to do what I want or is it
just not possible?

One way could be to use to Boost binders (don't know if that works, but
probably, else I would probably have remembered).
That's out until I can convince boss Boost good.

Perhaps you can convince your boss that TR1 is good, since it will be
part of the next C++ Standard. And you can now get a version that works
out of the box with VC++ from us.
I tried my best doing this and it just doesn't seem to work with
certain types of bosses unless the std library comes bundled with some
high & mighty version of the VC++ compiler (9.0 perhaps?). Does
Dinkumware's TR1 library compile with VC++ 8.0?

Jul 15 '06 #8
"Dilip" <rd*****@lycos.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
P.J. Plauger wrote:
>"Noah Roberts" <ro**********@gmail.comwrote in message
news:11**********************@h48g2000cwc.googleg roups.com...
Alf P. Steinbach wrote:
* Noah Roberts:
#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; }
};

int main(int argc, char* argv[])
{
std::vector<Xxes(10);

X x(10);

std::vector<X>::iterator it = std::find_if(xes.begin(), xes.end(),
std::bind2nd(std::mem_fun(&X::eq), x));

return 0;
}

Compilation result:
1>c:\program files\microsoft visual studio
8\vc\include\functional(312)
: error C2529: '_Right' : reference to reference is illegal
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_argument_type& _Right)
: op(_Func), value(_Right)
{ // construct from functor and right operand
}

This would follow...

So, what is the correct and portable way to do what I want or is it
just not possible?

One way could be to use to Boost binders (don't know if that works,
but
probably, else I would probably have remembered).

That's out until I can convince boss Boost good.

Perhaps you can convince your boss that TR1 is good, since it will be
part of the next C++ Standard. And you can now get a version that works
out of the box with VC++ from us.

I tried my best doing this and it just doesn't seem to work with
certain types of bosses unless the std library comes bundled with some
high & mighty version of the VC++ compiler (9.0 perhaps?).
We're working on that.
Does
Dinkumware's TR1 library compile with VC++ 8.0?
Yes, even the Express Edition.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 15 '06 #9

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...
5
4917
by: Old Wolf | last post by:
I have a member function that acts on an object. I would also like to have a member function that acts on a container of such objects, using std::for_each. I tried: #include <algorithm>...
2
1986
by: Robbie Hatley | last post by:
I've got a function that I use a lot when making utility programs that need to do the same thing to every directory in a tree. Its prototype is: unsigned long int CursDirs (void Func(void)); ...
4
3100
by: ShaneG | last post by:
We have ptr_fun to handle functions, mem_fun to handle member functions that will be called through a pointer, and mem_fun_ref to handle member functions that will be called through a reference. ...
3
2362
by: Chris Roth | last post by:
I have a vector (v) containing objects of class C. class C { private: double d; public: void foo( B& b ); };
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
1830
by: responsible | last post by:
Hi, Digging through the STL source code, i found this gem.. template <class _Operation, class _Tp> inline binder2nd<_Operation> bind2nd(const _Operation& __fn, const _Tp& __x) { typedef...
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
7171
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,...
1
6893
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
7386
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...
1
4918
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
4599
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
3098
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
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
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
664
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.