Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 708
abraham_hilbert Avatar asked Oct 30 '22 17:10

abraham_hilbert


1 Answers

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)

like image 105
krzaq Avatar answered Nov 15 '22 06:11

krzaq