468,491 Members | 1,950 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

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 4303

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by red floyd | last post: by
5 posts views Thread by Old Wolf | last post: by
2 posts views Thread by Robbie Hatley | last post: by
3 posts views Thread by Chris Roth | last post: by
2 posts views Thread by benben | last post: by
3 posts views Thread by Bruintje Beer | last post: by
4 posts views Thread by responsible | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by theflame83 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.