Re: [boost] Discovering C++11 lambda argument and result types to construct boost::function

On comp.lang.c++.moderated I talked to Daniel Krügler about how to discover argument and result types from C++11 lambdas in a template context. The thread has the title "How to discover argument and result types from lambda for constructing std::function?"
https://groups.google.com/d/topic/comp.lang.c++.moderated/qaMECIUlJp0...<http://www.google.com/url?sa=D&q=https://groups.google.com/d/topic/comp.lang.c%2B%2B.moderated/qaMECIUlJp0/discussion&usg=AFQjCNGnQG8_tRijTxietu-3OCbw3S-7xQ> The task is to provide a function template "make_function" that takes a C++11 lambda expression and returns a boost::function with the correct signature to call the lambda. This may be extended to accept any other callable type.
The task you are attempting to solve looks very unusual to me. All the usages of a lambda I encountered so far are that you know the type of a callback in advance and define the lambda so that it matches the signature of the targeted callback. Sorry if this question is silly, but could you give me an example of where this deduction mechanism could be used? Regards, &rzej

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi! Am 10.02.12 09:39, schrieb Andrzej Krzemienski:
Sorry if this question is silly, but could you give me an example of where this deduction mechanism could be used?
The example I gave on comp.lang.c++.moderated is quite plain. The make_function is used to construct a class template instatiation. I think it compares to std::make_pair. I'll give another example below. What I'm trying to work with is a lazy computation. My class template Lazy<T> contains a variant over the type T and a function<T()> that can produce a value of that type. template<typename T> struct Lazy<T> { typedef T value_type; typedef function<value_type()> lazy_function_type; typedef variant<value_type, lazy_function_type> data_type; data_type data; //...ctors... value_type& get() { if(auto* const p = boost::get<lazy_function_type>(&data)) { data = (*p)(); } return boost::get<value_type>(data); } }; The real task now is to create a make_lazy() function that constructs a Lazy<T> from a lambda expression. Even when the target type is somehow known there is still an advantage in convenience, esp. with "auto var = make_lazy(...)." When calling template functions that take Lazy<T> for some T the type deduction would not work with just a lambda expression. I hope I could show to you that there is some usecase for make_function. Frank -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.17 (Darwin) Comment: GPGTools - http://gpgtools.org Comment: keyserver x-hkp://pool.sks-keyservers.net iEYEARECAAYFAk82tzoACgkQhAOUmAZhnmrVYgCfUlN9FgIwgg0EqCbObBtNR2DR x0cAn14TOlJh5JotT7DI4Kv22uCP9+UJ =l6bv -----END PGP SIGNATURE-----
participants (2)
-
Andrzej Krzemienski
-
Frank Birbacher