samedi 24 octobre 2020

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

How does one obtain the return type of a function (passed into a higher-order function/class) in this fashion:

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

My hunch is that decltype or declval ought to be in the picture, but I have had no luck tinkering with it so far.

More thorough context:

struct Poop
{
    float x;
    int y;
}

Poop Digest()
{
    Poop myPoop{ 3.0f, 2 };
    return myPoop;
}

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

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

Aucun commentaire:

Enregistrer un commentaire