By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

Namespace of lambda library/how to use _1 with members

P: n/a
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<MyStructm_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!

Jul 6 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Tigera wrote:
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

Jul 6 '06 #2

P: n/a
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::operator==(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::operator==(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...

Steve Hicks wrote:
Tigera wrote:
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
Jul 6 '06 #3

P: n/a
Tigera wrote:
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

Jul 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.