From: Jerzie.Klenchier on
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, double> my_tuple_type;
std::deque<my_tuple_type> my_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<int> v_list;
std::sort(v_list.begin(),v_list.end(), boost::lambda::_1 <
boost::lambda::_2);
}


Jerzie

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Mathias Gaunard on
On 11 avr, 19:33, Jerzie.Klench...(a)gmail.com wrote:

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

That should be in the Boost.Lambda documentation. You obviously cannot
use the dot operator on placeholders for obvious reasons.
You must use bind.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: partow on
If you implement the following selector in scope of the
comparator you intend to use:

template< int N, class Tup >
inline
typename boost::tuples::element<N,Tup>::type& getN(Tup& t)
{
return boost::tuples::get<N>(t);
}

You can then proceed to do the following:

template<typename T>
inline bool compare(const T& t1,const T& t2)
{
return t1 < t2;
}

int main(void)
{
typedef boost::tuples::tuple<int,double> my_tuple_type;

std::vector<my_tuple_type> v;

{
using namespace boost;
using namespace boost::tuples;

std::sort(v.begin(),v.end(),
bind<bool>(compare<element<0,my_tuple_type>::type>,
bind(&getN<0,my_tuple_type>,::_1),
bind(&getN<0,my_tuple_type>,::_2)));

// or if you're really keen on using lambda
std::sort(v.begin(),v.end(),
bind<bool>((lambda::_1 < lambda::_2),
bind(&getN<0,my_tuple_type>,::_1),
bind(&getN<0,my_tuple_type>,::_2)));
}
return 0;
}

A note of caution - The technical realty of this is that even
though it might look "cool" to be using lambda and bind, the
efficiency of the executable-code produced by the compiler-linker
combo (in today's terms not sure what the future will be like) when
compared to a much simpler yet restrictive comparator, is for the
most part unacceptable.


Arash Partow
__________________________________________________
Be one who knows what they don't know,
Instead of being one who knows not what they don't know,
Thinking they know everything about all things.
http://www.partow.net


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Carl Barron on
In article
<a914bd40-1930-4fd1-aacc-a4990510dd39(a)1g2000prf.googlegroups.com>,
<partow(a)gmail.com> wrote:

>
> A note of caution - The technical realty of this is that even
> though it might look "cool" to be using lambda and bind, the
> efficiency of the executable-code produced by the compiler-linker
> combo (in today's terms not sure what the future will be like) when
> compared to a much simpler yet restrictive comparator, is for the
> most part unacceptable.

Not to memtion that bind does not work directly on boost::get<N>(const
Tuple &) so a wrapper is needed, If I have to write a wrapper, then I
might as well write a comparison op. that does it all without bind or
lambda...
boost::bind(&getN<0,my_tuple_type>,_1) <
boost::bind(&getN<0,my_tuple_type>,_2)

should work but why bother using bind on a special wrapper unless this
idiom is used elsewhere in the code, with different binary or unary
functors.

further if the order of the second element does not matter then
a straight sort(v.begin(),v.end()) will work if
boost/tuple/tuple_comparison.hpp is included. It would sort equal
first element values with regard to the second element values, etc.

Mileage may vary but what is unacceptable is sometimes acceptable:)

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]