Search code examples
c++c++14callabletype-deduction

Deduction of result type of callable


I try to deduce the type of callable template parameter, unfortunately without success:

template<typename callable, typename T_out >
class A
{};

template<typename callable>
auto make_A( callable f )
{
  return A<callable, typename std::result_of_t<callable> >{ f };
}

int main()
{
  make_A( []( float f ){ return f;} );
}

The code above causes the following error:

error: implicit instantiation of undefined template 'std::__1::result_of<(lambda at /Users/arirasch/WWU/dev/xcode/tests/tests/main.cpp:31:11)>'
template <class _Tp> using result_of_t = typename result_of<_Tp>::type;

Does anyone know how to fix it?

Many thanks in advance.


Solution

  • You need to pass the argument list to std::result_of, otherwise it's impossible to tell the return type (operator() can be overloaded, after all).

    return A<callable, std::result_of_t<callable(float)> >{ f }
    

    (provided A<callable, std::result_of_t<callable(float)> can be constructed with f, which isn't the case for the example)