如何获取传递给模板函数或 class 的函数的 return 类型?

How to obtain the return type of a function passed into a templated function or class?

如何以这种方式获得函数的 return 类型(传递到高阶 function/class):

template <typename F>
auto DoSomething(F&& func) -> /* whatever type func returns */
{
    // whatever...
    return /* something that is func's type */
}

编辑:特别是如果 func 需要 T 类型的参数。

我的直觉是 decltypedeclval 应该在图片中,但到目前为止我还没有运气来修补它。

更详尽的上下文:

struct Poop
{
    float x;
    int y;
}

Poop Digest(float a)
{
    Poop myPoop{ a, 42 };
    return myPoop;
}

template <typename F, typename T>
auto DoSomething(F&& func, T number) -> /* should be of type Poop  */
{
    // whatever... Digest(number)... whatever...
    return /* a Poop object */
}

int main()
{
    Poop smellyThing;
    smellyThing = DoSomething(Digest, 3.4f); // will work
}

确实,您可以这样使用 decltype

template <typename F, typename T>
auto DoSomething(F&& func, T number) -> decltype(func(number))
{
  // ...
  return {};
} 

这是一个demo