Namespace of lambda library/how to use _1 with members

T

Tigera

Hi!

I'm new to TR1, and I am trying to use the lambda library to simplify
finding a specific object within a vector. I'm using gcc-4.0.1, and I
can't figure out what namespace the placeholder for a variable (the
function object var) is in. Here is an abridged example of what I'm
trying to do:

#include <algorithm>
#include <tr1/functional>

using std::tr1::placeholder::_1;
using std::tr1::placeholder::var;
//I don't know what to use for var in std::tr1!

struct MyStruct
{
MyClass* occupant;
};


class MySystem
{
private:
vector<MyStruct> m_system;

public:
int FindByPointer(MyClass* target)
//Find the index of the target in the vector
{
return std::distance(m_system.begin(),
std::find_if(m_system.begin(), m_system.end(), (_1).occupant ==
var(target)));
}

};

Unfortunately, I receive errors when this code compiles, mentioning
that there is no occupant member in std::tr1::placeholder::_1, and that
var hasn't been declared.

Would someone please tell me:
1. How do I refer to a member/property of a user-defined class when
using _1?
2. What namespace contains the "var" functor?

Thanks for your help!
 
S

Steve Hicks

Tigera said:
Hi!

#include <algorithm>
#include <tr1/functional>

using std::tr1::placeholder::_1;
using std::tr1::placeholder::var;
//I don't know what to use for var in std::tr1!

I have std::tr1::placeholders. But still no var() there with those
#includes. I don't know where to find it, either.
std::find_if(m_system.begin(), m_system.end(), (_1).occupant ==
var(target)));

Have you had success with a simpler lambda function, like simple
assignment _1 = 1?
Unfortunately, I receive errors when this code compiles, mentioning
that there is no occupant member in std::tr1::placeholder::_1, and that
var hasn't been declared.
Would someone please tell me:
1. How do I refer to a member/property of a user-defined class when
using _1?

Indeed, _1 has no member occupant. You'd probably need to use bind()
or var() to access it with a pointer-to-member, but I can't seem to
find the specs for tr1 anywhere to look it up and I can't compile it
myself to try anything.
2. What namespace contains the "var" functor?

???

-steve
 
T

Tigera

I've tried the simpler assignment of the whole object as opposed to
just member occupant, as you suggested, though that doesn't return a
bool, so I used it with transform() instead. It doesn't work there,
either. I also tried to define an equality operator (==) within the
struct. In each case, I get similar errors:

battlefield.cpp:81: error: no match for 'operator==' in
'std::tr1::placeholders::<unnamed>::_1 == a'
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/tr1/boost_shared_ptr.h:401:
note: candidates are: bool std::tr1::eek:perator==(const
std::tr1::weak_count&, const std::tr1::weak_count&)
/usr/lib/gcc/i486-linux-gnu/4.0.3/../../../../include/c++/4.0.3/tr1/boost_shared_ptr.h:315:
note: bool std::tr1::eek:perator==(const
std::tr1::shared_count&, const std::tr1::shared_count&)

I defined the variable "a" to be just a plain instance of MyClass. In
the case of the assignment, the compiler complains that there is no
operator= in std::tr1::placeholders::<unnamed>::_1.

Perhaps I still need to qualify variable a with var()?

Thanks...
 
S

Steve Hicks

Tigera said:
I've tried the simpler assignment of the whole object as opposed to
just member occupant, as you suggested, though that doesn't return a
bool, so I used it with transform() instead. It doesn't work there,
either. I also tried to define an equality operator (==) within the
struct. In each case, I get similar errors:

I finally figured out what these placeholders are useful for.
Evidently, tr1 does not actually include the full lambda library. From
what I could tell, the only use for the placeholders is in bind()
expressions. So the following code compiles and runs successfully:

#include <tr1/functional>
using std::tr1::placeholders::_1;
using std::tr1::bind;
using std::tr1::function;

void increment(int &x,int y) { x+=y; }

int main() {
int x=0;
function<void(int &)> f = bind(increment,_1,10);
f(x);
assert(x==10);
return 0;
}

This is a good bit more useful than bind1st and bind2nd, but it's still
a far cry from boost::lambda. I really do hope the rest of the library
makes it into c++0x.

steve hicks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top