Is it possible to get the argument's type of lambda_functor?

With the boost::function<>::argN_type I can get boost::function's argument type. How can I do with the boost::lambda_functor? Thanks.

I Wei wrote:
With the boost::function<>::argN_type I can get boost::function's argument type. How can I do with the boost::lambda_functor?
You can't because there is no such thing. A lambda functor such as _1 + 2 can accept (almost) any argument x for which x+1 makes sense. What are you trying to do?

On 7/9/07, Peter Dimov <pdimov@pdimov.com> wrote:
I Wei wrote:
With the boost::function<>::argN_type I can get boost::function's argument type. How can I do with the boost::lambda_functor?
You can't because there is no such thing. A lambda functor such as _1 + 2 can accept (almost) any argument x for which x+1 makes sense. What are you trying to do?
This is my situation: #include "boost/function.hpp" #include "boost/lambda/bind.hpp" #include "boost/shared_ptr.hpp" template <bool b> struct getter { template<typename T1, typename T2> void do_get(int n, T2 f) { // do something; } }; template <> struct getter<true> { template<typename T1, typename T2> void do_get(int n, T2 f) { // do something else; } }; template <typename T1, typename T2> void func(int n, T2 f) { typedef typename T2::arg1_type arg1_type; getter< boost::is_same< arg1_type, boost::shared_ptr<T1> >::value >do_get<T1, boost::function<void(arg1_type)> >(n, f); } class Foo { }; void foo(int n, Foo t) { } int main() { func<Foo>(1, boost::lambda::bind(&foo, 0, boost::lambda::_1)); return 0; } because the boost::lambda::bind return a lambda_functor, the " typedef typename T2::arg1_type arg1_type;" is wrong, if I add the second template argument to the caller func as func<Foo, boost::function<void(Foo)> >, everything will be ok. But there are a lot of similar calls with different second template argument, I want the compiler deduce it by the caller function's actual argument. How can I do that?

On 7/8/07, I Wei <i.c.code@gmail.com> wrote:
On 7/9/07, Peter Dimov <pdimov@pdimov.com> wrote:
I Wei wrote:
With the boost::function<>::argN_type I can get boost::function's argument type. How can I do with the boost::lambda_functor?
You can't because there is no such thing. A lambda functor such as _1 + 2 can accept (almost) any argument x for which x+1 makes sense. What are you trying to do?
This is my situation: [snip]
template <typename T1, typename T2> void func(int n, T2 f) { typedef typename T2::arg1_type arg1_type;
getter< boost::is_same< arg1_type, boost::shared_ptr<T1> >::value >do_get<T1, boost::function<void(arg1_type)> >(n, f); } [snip] But there are a lot of similar calls with different second template argument, I want the compiler deduce it by the caller function's actual argument. How can I do that?
I'm just guessing here (your example never actually invokes the lambda functor), but I think you want to change func above to something like... template <typename T1, typename T2> void func(T1 n, T2 f) { typedef T1 arg1_type; // ... } ... if you want to deduce the type of n. I am not sure that you need to use boost::function at all. Also, this seems like a very convoluted way of dispatching a function based on whether or not it's called with a smart pointer. Why not overload do_get for smart_pointer rather than specializing getter for the value of is_same? Daniel

On 7/9/07, Daniel Walker <daniel.j.walker@gmail.com> wrote:
On 7/8/07, I Wei <i.c.code@gmail.com> wrote:
On 7/9/07, Peter Dimov <pdimov@pdimov.com> wrote:
I Wei wrote:
With the boost::function<>::argN_type I can get boost::function's argument type. How can I do with the boost::lambda_functor?
You can't because there is no such thing. A lambda functor such as _1 + 2 can accept (almost) any argument x for which x+1 makes sense. What are you trying to do?
This is my situation: [snip]
template <typename T1, typename T2> void func(int n, T2 f) { typedef typename T2::arg1_type arg1_type;
getter< boost::is_same< arg1_type, boost::shared_ptr<T1> >::value >do_get<T1, boost::function<void(arg1_type)> >(n, f); } [snip] But there are a lot of similar calls with different second template argument, I want the compiler deduce it by the caller function's actual argument. How can I do that?
I'm just guessing here (your example never actually invokes the lambda functor), but I think you want to change func above to something like...
template <typename T1, typename T2> void func(T1 n, T2 f) { typedef T1 arg1_type; // ... }
... if you want to deduce the type of n.
I am not sure that you need to use boost::function at all. Also, this seems like a very convoluted way of dispatching a function based on whether or not it's called with a smart pointer. Why not overload do_get for smart_pointer rather than specializing getter for the value of is_same?
Daniel _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Dear Daniel, The lambda_functor is do invoked, which is the return value of boost::lambda::bind. I thought I could use less template argument, if the compiler would deduce the argument type of lambda_functor. And I think it is impossible now. If the compiler can't deduce the argument type from a function pointer, it should not be able to do on the lambda_functor. Wei

Actually, the compiler can deduce argument types from a function pointer. You can use function_traits combined with typeof. But you can't deduce argument types from lambda_functor because a lambda_functor did not have any argument type before it invoke. On 7/10/07, I Wei <i.c.code@gmail.com> wrote:
I thought I could use less template argument, if the compiler would deduce the argument type of lambda_functor. And I think it is impossible now. If the compiler can't deduce the argument type from a function pointer, it should not be able to do on the lambda_functor.
Wei _______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

on Sun Jul 08 2007, "I Wei" <i.c.code-AT-gmail.com> wrote:
With the boost::function<>::argN_type I can get boost::function's argument type. How can I do with the boost::lambda_functor?
You can't. Think about it by looking at the simplest lambda expression: _1 What is the argument type? It could be anything. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com The Astoria Seminar ==> http://www.astoriaseminar.com
participants (5)
-
Atry
-
Daniel Walker
-
David Abrahams
-
I Wei
-
Peter Dimov