Boost.Lambda question

Hi all, I am wonderring if there is an easy way to determine the type of a lambda functor, kind of lambda-specific typeof facility. I do realize that the primary purpose of lambda is to create temporary functors to pass to STL algorithms. On the other hand, there are contexts where functors need to be stored, such as in std::set, etc. One might want to use Lambda to generate such a functor, but without knowing its type it would be of limited use (of course one can directly specify the type of such functor, but this would be rather tedious). Has anything been done by anybody in this regard? Thanks, Arkadiy

On Wed, Apr 07, 2004 at 08:59:42PM -0400, Arkadiy Vertleyb wrote:
I am wonderring if there is an easy way to determine the type of a lambda functor, kind of lambda-specific typeof facility. I do realize that the primary purpose of lambda is to create temporary functors to pass to STL algorithms.
On the other hand, there are contexts where functors need to be stored, such as in std::set, etc.
I assume you mean for the comparator, not storing a set of functors.
Has anything been done by anybody in this regard?
I've often used Boost.Function for this, though I'm interested in approaches that don't involve the function-pointer-like overhead: typedef Some_Type SetElementType; typedef boost::function< bool (const SetElementType& , const SetElementType&) > SetElementComparator; typedef std::set< SetElementType, SetElementComparator > SetType; Best, -- Shannon Stewman | Let us walk through the waning night, Caught in a whirlpool, | As dawn-rays tickle our toes, the dew soothes A quartering act: | Our blistered soles, and damp bones stir Solitude or society? | As crimson cracks under the blue-grey sky.

Boost.Function does what I need. Thanks a lot for the tip. Arkadiy
I am wonderring if there is an easy way to determine the type of a lambda functor, kind of lambda-specific typeof facility. I do realize that the primary purpose of lambda is to create temporary functors to pass to STL algorithms.
On the other hand, there are contexts where functors need to be stored, such as in std::set, etc.
I assume you mean for the comparator, not storing a set of functors.
Has anything been done by anybody in this regard?
I've often used Boost.Function for this, though I'm interested in approaches that don't involve the function-pointer-like overhead:
typedef Some_Type SetElementType; typedef boost::function< bool (const SetElementType& , const SetElementType&) > SetElementComparator; typedef std::set< SetElementType, SetElementComparator > SetType;
Best,
-- Shannon Stewman | Let us walk through the waning night, Caught in a whirlpool, | As dawn-rays tickle our toes, the dew soothes A quartering act: | Our blistered soles, and damp bones stir Solitude or society? | As crimson cracks under the blue-grey sky. _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On Wed, Apr 07, 2004 at 08:59:42PM -0400, Arkadiy Vertleyb wrote:
I am wonderring if there is an easy way to determine the type of a lambda functor, kind of lambda-specific typeof facility. I do realize that the primary purpose of lambda is to create temporary functors to pass to STL algorithms. On the other hand, there are contexts where functors need to be stored, such as in std::set, etc. One might want to use Lambda to generate such a functor, but without knowing its type it would be of limited use (of course one can directly specify the type of such functor, but this would be rather tedious).
Has anything been done by anybody in this regard?
I don't know that Lambda has anything like this. FC++ does: http://www.cc.gatech.edu/~yannis/fc++/boostpaper/fcpp.sectlambda.html#id2708... Perhaps if/when work on a new "FP core" built atop Fusion happens, these considerations can be built in. In the meantime, it might not be too difficult (though maybe a lot of "busy-work") to add this alongside the existing Lambda implementation. I dunno enough about the specifics to be sure, but Jaakko or Peter or Joel would probably know. (And of course, as someone else mentioned, you can always use Boost.Function if you are able/willing to add a layer of indirection and "forget" the most specific type information.) -- -Brian McNamara (lorgon@cc.gatech.edu)

Brian McNamara <lorgon <at> cc.gatech.edu> writes:
On Wed, Apr 07, 2004 at 08:59:42PM -0400, Arkadiy Vertleyb wrote:
I am wonderring if there is an easy way to determine the type of a lambda functor, kind of lambda-specific typeof facility. I do realize that the primary purpose of lambda is to create temporary functors to pass to STL algorithms. On the other hand, there are contexts where functors need to be stored, such as in std::set, etc. One might want to use Lambda to generate such a functor, but without knowing its type it would be of limited use (of course one can directly specify the type of such functor, but this would be rather tedious).
Has anything been done by anybody in this regard?
I don't know that Lambda has anything like this. FC++ does:
http://www.cc.gatech.edu/~yannis/fc++/boostpaper/fcpp.sectlambda.html#id2708...
I think the possibility of having the type of a lambda expression is extremely interesting. IMHO, this facility can be conceptualized as a "static lambda" library. Let me ellaborate: Consider std::sort. This function takes an extra functor in charge of doing the comparison work, for which Boost.Lambda nicely fits the bill: std::sort(it0,it1,_1<_2); // trivial comparison. std::set, OTOH, also takes a comparison predicate, though this is commonly passed by type rather than as an expression: std::set<int,std::less<int> > Here, we can consider std::less as the static version of _1<_2, with the difference that the <int> has to be explicitly stated. Generally speaking, a given class template T is the static version of a lambda expression expr if T<Arg1,...Argn>()(arg1,...,argn) is the same as expr(arg1,...,argn) for argi of type Argi. So it would be nice if Boost.Lambda is augmented with more or less comfortable syntax to produce static lambda types. I'm thinking of having something as namespace sl=boost::lambda::static; std::set< int, sl::less<sl::_1,sl::_2>::type // equivalent to _1<_2 <int,int> // instantiated for int types
;
std::set< int*, sl::less< sl::deref<sl::_1>, // eq. to *_1<*_2 sl::deref<sl::_2>
::type <int*,int*> // instantiated for int* ;
Maybe this is quite a lot of work, but IMHO the potential of it is great. I don't know wheter MPL has already the possibility of doing something like this (I guess it doesn't), or on the contrary this static lambda library can be regarded as a cooperative add-on to MPL. Comments? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo

"Joaquin M Lopez Munoz" <joaquin@tid.es> wrote
I think the possibility of having the type of a lambda expression is extremely interesting. IMHO, this facility can be conceptualized as a "static lambda" library...
So it would be nice if Boost.Lambda is augmented with more or less comfortable syntax to produce static lambda types...
Maybe this is quite a lot of work, but IMHO the potential of it is great. I don't know wheter MPL has already the possibility of doing something like this (I guess it doesn't), or on the contrary this static lambda library can be regarded as a cooperative add-on to MPL. Comments?
As far as Lambda is concerned, Boost.Function can abstract you from the functor type (although at the expence of some overhead): std::set<int, boost::function<bool(int)> > s(_1 < _2); But in general, the technique to determine the type of an expression tree, where each leave is a pre-registered type, and each other node is an instance of a pre-registered template would be extremely interesting for many libraries (and for RTL in particular). If you have any specific thoughts about this, I would be very interested to know... Regards, Arkadiy

Hi Arkadiy, Arkadiy Vertleyb ha escrito: [...]
As far as Lambda is concerned, Boost.Function can abstract you from the functor type (although at the expence of some overhead):
std::set<int, boost::function<bool(int)> > s(_1 < _2);
Yep, this has been previously discussed. Three problems here: 1) boost::function adds an extra indirection penalty. 2) Having to provide the function at iintialization time is cumbersome. 3) some valuable type info is lost. To continue with the std::set example, set<T,boost::function<bool(const T&,const T&,)> flattens out every possible lambda expression we might plug in. This might not be desirable. Besides, I think such a static lambda library could be useful for more far-fetched purposes, along the line of enriching the code-producing capabilities of MPL. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (5)
-
Arkadiy Vertleyb
-
Brian McNamara
-
Joaquin M Lopez Munoz
-
Joaquín Mª López Muñoz
-
Shannon Stewman