0

Possible Duplicate:
Is it possible to figure out the parameter type and return type of a lambda?

I'd like to make a function that accepts an integer and an arbitrary function (with arbitrary return type and arguments), to be called like so:

server.bind(tag, [&](std::string greeting) { return greeting + " earth."; });

Any ideas on how to set up the templates to get the lambda's return type and the types of the lambda's arguments deducted?

The following code is as close as I could get, but it fails template argument deduction with the above code:

template <typename Return, typename... Args>
void service::bind(uint16_t tag, 
                   std::function<Return (Args...)> function)
{
    ...
}
Community
  • 1
  • 1
vedosity
  • 720
  • 4
  • 15
  • 2
    Don't restrict what kind of function / functor you accept in your `service::bind`, make it a simple template parameter and function parameter: `template void service::bind(uint16_t tag, F f)`. – Xeo Jul 16 '12 at 08:57
  • 1
    @Xeo I need the return type and argument types in the body of the function. The top answer of KennyTM's link might help with that though... – vedosity Jul 16 '12 at 09:01
  • 2
    Why do you need the argument type? You should know what you want to pass to the function you accept. And once you know that, the return type is easily figured out with `decltype(f(arg))`. – Xeo Jul 16 '12 at 09:03
  • @vedosity If you accept an arbitrary functor as argument and want to access return type/argument types you will need to use indirection through traits to handle lambda, `std::function` and normal functors uniformly. – pmr Jul 16 '12 at 09:03

0 Answers0