473,406 Members | 2,220 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,406 software developers and data experts.

Lambda and Tuple question

Hi all,

I'm having some difficulty in getting the following piece of code to
compile:

#include <iostream>
#include <deque>
#include <algorithm>
#include <boost/tuple/tuple.hpp>
#include <boost/lambda/lambda.hpp>

int main(void)
{
typedef boost::tuples::tuple<unsigned int, doublemy_tuple_type;
std::deque<my_tuple_typemy_list;

std::sort(my_list.begin(),my_list.end(),
(boost::lambda::_1).get<0>() <
(boost::lambda::_2).get<0>());

return 0;

}

my understanding of the lambda place-holders was that they represent
the value_type of the iterators in the above example. A simpler
example as below seems to work, just wondering why the above doesn't.

{
std::vector<intv_list;
std::sort(v_list.begin(),v_list.end(),
boost::lambda::_1 < boost::lambda::_2);

}

Jerzie
Jun 27 '08 #1
4 3127
Je**************@gmail.com wrote:
Hi all,

I'm having some difficulty in getting the following piece of code to
compile:

#include <iostream>
#include <deque>
#include <algorithm>
#include <boost/tuple/tuple.hpp>
#include <boost/lambda/lambda.hpp>

int main(void)
{
typedef boost::tuples::tuple<unsigned int, doublemy_tuple_type;
std::deque<my_tuple_typemy_list;

std::sort(my_list.begin(),my_list.end(),
(boost::lambda::_1).get<0>() <
(boost::lambda::_2).get<0>());

return 0;

}

my understanding of the lambda place-holders was that they represent
the value_type of the iterators in the above example. A simpler
example as below seems to work, just wondering why the above doesn't.
We cannot overload the dot-operator (what a shame!). Therefore, the magic
functor _1 cannot forward member functions.

You can try to use bind to make it work. So, instead of _1.get<0>, you would
have a monstrosity like:

bind( &whatever_type::get<0>, _1 )

(or worse: I did not try it.)
>
{
std::vector<intv_list;
std::sort(v_list.begin(),v_list.end(),
boost::lambda::_1 < boost::lambda::_2);

}
That works just fine because lambda does not run into limitations of the C++
core language (another thing that lambda cannot support elegantly is
some_vector[_1] since operator[] has to be a member function).
Best

Kai-Uwe Bux
Jun 27 '08 #2
Kai-Uwe Bux wrote:
We cannot overload the dot-operator (what a shame!). Therefore, the magic
functor _1 cannot forward member functions.

You can try to use bind to make it work. So, instead of _1.get<0>, you would
have a monstrosity like:

bind( &whatever_type::get<0>, _1 )

(or worse: I did not try it.)
What about the old trick

(*&_1).get<0>()

Normally this should work too. Of course, it implies that the operator&
of the object behind _1 is not overloaded in some magic way.

(I did not test it, since I have no boost::lambda currently running here.)

Marcel
Jun 27 '08 #3
The boost lambda library is wonderful in theory, but IMO a nightmare
in practice.

It's a seriously clever piece of software, and the authors should take
full credit for the ingenuity of it, but the time scale for using it
goes something like:

* for loop with iterators
* algorithm library with custom function object (or functional
library) ~ 2-3 times longer to program, similar performance as for
loop
* Boost Lambda library ~ a good 10 times longer than a for loop (if
ever) to program, significantly worse performance

I know it looks really nifty to have a bit of lambda in the code, but
I'm just curious to know if anyone has a compelling (non-academic)
reason to wrestle with the beast that is the BLL?

Brian
Jun 27 '08 #4
brian tyler <br*********@gmail.comwrites:
The boost lambda library is wonderful in theory, but IMO a nightmare
in practice.

It's a seriously clever piece of software, and the authors should take
full credit for the ingenuity of it, but the time scale for using it
goes something like:

* for loop with iterators
* algorithm library with custom function object (or functional
library) ~ 2-3 times longer to program, similar performance as for
loop
* Boost Lambda library ~ a good 10 times longer than a for loop (if
ever) to program, significantly worse performance

I know it looks really nifty to have a bit of lambda in the code, but
I'm just curious to know if anyone has a compelling (non-academic)
reason to wrestle with the beast that is the BLL?
Lisp envy?
Or otherwise, working with an IDE where creating a new function or
method is significantly more time consuming than just typing the
function signature in a text editor, and therefore where it would be
advantageous to be able to use anonymous functions.
--
__Pascal Bourguignon__
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

63
by: Stephen Thorne | last post by:
Hi guys, I'm a little worried about the expected disappearance of lambda in python3000. I've had my brain badly broken by functional programming in the past, and I would hate to see things...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
18
by: talin at acm dot org | last post by:
I've been reading about how "lambda" is going away in Python 3000 (or at least, that's the stated intent), and while I agree for the most part with the reasoning, at the same time I'd be sad to see...
25
by: Russell | last post by:
I want my code to be Python 3000 compliant, and hear that lambda is being eliminated. The problem is that I want to partially bind an existing function with a value "foo" that isn't known until...
3
by: Boris Borcic | last post by:
>>x = (lambda : ((yield 666),(yield 777),(yield 888)))() 666 777 888 (None, None, None) 666 777 888 Traceback (most recent call last):
0
by: Jerzie.Klenchier | last post by:
Hi all, I'm having some difficulty in getting the following piece of code to compile: #include <iostream> #include <deque> #include <algorithm> #include <boost/tuple/tuple.hpp> #include...
5
by: I V | last post by:
On Sun, 25 May 2008 13:43:15 +0200, Martin Manns wrote: With Ivan's approach, you lose access to the actual lambdas, so you need to create a new function and then modify its code object to...
1
by: Tim H | last post by:
Compiling with g++ 4: This line: if_then_else_return(_1 == 0, 64, _1) When called with a bignum class as an argument yields: /usr/include/boost/lambda/if.hpp: In member function 'RET...
20
by: raylopez99 | last post by:
Took a look at all the fuss about "lambda expressions" from Jon Skeet's excellent book "C# in Depth". Jon has an example, reproduced below (excerpt) on lambda expressions. My n00b take: it's...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.