Boost logo

Boost Users :

From: Kirit Sælensminde (kirit.saelensminde_at_[hidden])
Date: 2007-12-15 00:53:42


Guo Xu wrote:
> I want to implement the grammar like this,
> to create template parameter dynamicly,
>
> set< int, ( _1 > _2 ) > intset;
>
> can lambda do it ,and how to ?

Templates are compile time entities so the parameters have to be fully
specified at compile time. For the specific example you have
std::greater should work for you:

set< int, std::greater< int > > intset;

More generally, you can write your own custom comparators and you can
write one of those that takes a lambda at run time to use.

There's an example comparator using Boost.Regex at
http://www.boostcookbook.com/Recipe:/1235053 which shows the general
idea. The important points are that it derives from
std::binary_function<> and implements an operator() which returns a bool
and takes two instances of the value type that are being compared.

To use a lambda, you'd want a constructor that took a boost::function<
bool ( int, int ) > and stored it for future use. Changing the lambda
after the set has started to be populated would almost certainly be a
terrible idea.

K

-- 
http://www.kirit.com/

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net