Why does std::result_of not work with lambdas?

c++, c++11, gcc, lambda

Solution

Your code's not valid C++ because function parameter types cannot be `auto`, this syntax has been proposed for Concepts Lite and may become part of the language in the future.

`result_of` needs a call expression from which it will deduce the return type.

Fixing both of these, your code becomes

template<typename F>
auto call(F const& f) -> typename std::result_of<decltype(f)()>::type
//                    or typename std::result_of<F()>::type
{
  return f();
}

Or you could just use

template<typename F>
auto call(F const& f) -> decltype(f())
{
  return f();
}

Live demo

I think your original code should compile if you fix the `result_of` expression, but it doesn't on gcc-4.9 or 5.0; maybe this is a bug with the gcc extension that allows parameter types to be `auto`

// This fails to compile
auto call3(const auto& f) -> typename std::result_of<decltype(f)()>::type
{
  return f();
}

Problem

I managed to reduce my case to the following simplest piece of code: ``` #include <type_traits> auto call(const auto& f) -> typename std::result_of<decltype(f)()>::type { return f(); } int main() { return call([] { return 0; }); } ``` Neither gcc-4.9.2 and gcc-5.0.0 compile! Both think that "call" should be returning a lambda function! The don't figure out that "call" returns an int. Is this a bug in the compiler or is my c++ off? Many thanks.

Original source