473,385 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Namespace of lambda library/how to use _1 with members

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
3 2142
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Joshua Ginsberg | last post by:
Howdy -- I have a class that has an attribute that is a dictionary that contains an object that has a kword argument that is a lambda. Confused yet? Simplified example: import copy class...
5
by: Genboy | last post by:
My "VIS" Website, which is a C# site created in VS.NET, Framework 1.1, is no longer compiling for me via the command line. As I have done 600 times in the last year and a half, I can compile to...
6
by: ryan.d.rembaum | last post by:
Hello, I have code that I wish to use in many web applications. Basically sort of stand utility stuff. So from Visual Studio Project I select add a component and chose Component Class. Lets...
4
by: | last post by:
When I add a new module in the project explorer pane, the wizard inserts a Module1 scope, so any variables I will put there can be accessed with a qulification, e.g. dim a as integer will be...
32
by: toolmaster | last post by:
Since many of the modern computer languages have built-in namespace features, I can't understand why not add this feature into standard C. I've heard many people complain of the lacking of...
4
by: Xah Lee | last post by:
A Lambda Logo Tour (and why LISP languages using λ as logo should not be looked upon kindly) Xah Lee, 2002-02 Dear lispers, The lambda character λ, always struck a awe in me, as with...
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
1
by: Brandt Dusthimer | last post by:
Hi all. I was wondering, is there any way of using the boost lambda types as an index for a container? For example: for_each(indexList.begin(), indexList.end(), ( somelist.someFunction() ));...
2
by: PengYu.UT | last post by:
Hi, I have the following code. However, the lambda expression becomes too complicated, because I put "boost::lambda::" before _1 and _2. To make it more readable, I can uncomment the commented...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.