473,714 Members | 2,405 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<Xxe s(10);

X x(10);

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

return 0;
}

Compilation result:
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(312)
: error C2529: '_Right' : reference to reference is illegal
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_ar gument_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 4495

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<Xxe s(10);

X x(10);

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

return 0;
}

Compilation result:
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(312)
: error C2529: '_Right' : reference to reference is illegal
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_ar gument_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==(cons t X & other) const { return x == other.x; }
};

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

X x(10);

std::vector<X>: :iterator it = std::find_if(
xes.begin(),
xes.end(),
std::bind2nd(st d::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<Xxe s(10);

X x(10);

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

return 0;
}

Compilation result:
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(312)
: error C2529: '_Right' : reference to reference is illegal
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_ar gument_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==(cons t 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<Xxe s(10);
>
X x(10);
>
std::vector<X>: :iterator it = std::find_if(xe s.begin(), xes.end(),
std::bind2nd(st d::mem_fun(&X:: eq), x));
>
return 0;
}
>
Compilation result:
>
>
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(312)
: error C2529: '_Right' : reference to reference is illegal
>
>
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_ar gument_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==(cons t 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**********@g mail.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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<Xxe s(10);

X x(10);

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

return 0;
}

Compilation result:
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(312)
: error C2529: '_Right' : reference to reference is illegal
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_ar gument_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<Xxe s(10);

X x(10);

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

return 0;
}

Compilation result:
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(312)
: error C2529: '_Right' : reference to reference is illegal
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_ar gument_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==(cons t 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**********@g mail.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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<Xxe s(10);

X x(10);

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

return 0;
}

Compilation result:
1>c:\program files\microsoft visual studio 8\vc\include\fu nctional(312)
: error C2529: '_Right' : reference to reference is illegal
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_ar gument_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.goo glegroups.com.. .
P.J. Plauger wrote:
>"Noah Roberts" <ro**********@g mail.comwrote in message
news:11******* *************** @h48g2000cwc.go oglegroups.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<Xxe s(10);

X x(10);

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

return 0;
}

Compilation result:
1>c:\program files\microsoft visual studio
8\vc\include\fu nctional(312)
: error C2529: '_Right' : reference to reference is illegal
binder2nd(const _Fn2& _Func,
const typename _Fn2::second_ar gument_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
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>
5
4929
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> #include <functional> struct bar { template<typename T> void foo(T const &); template<typename InIt> void foo(InIt begin, InIt end)
2
1990
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)); This just applies the fuction Func to every subdirectory of the current directory. It works fine when I pass it pointers to regular void-void functions.
4
3111
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. First, why do we need to have seperate mem_fun/mem_fun_ref? The first returns a mem_fun_t, and the second a mem_fun_ref_t. But the only difference between mem_fun_t and mem_fun_ref_t is that they have different operator() methods. But since...
3
2369
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
1813
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
1842
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 typename _Operation::second_argument_type _Arg2_type; return binder2nd<_Operation>(__fn, _Arg2_type(__x)); }
4
6182
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
8810
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9316
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
9182
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
9078
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
7955
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
6638
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5959
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
4729
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3160
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

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.