c++decltyperesult-of

Why does operator () with type argument can be applied to type in the context of result_of?


As fas as I understand, result_of_t should be a type, that will be at the end of the evaluation of an expression. decltype(&foo) in the code below yields the type int (*)(int), but what does (int) outside of decltype?

#include <type_traits>

int foo(int) {
    return 0xdeadbeef;
}

int main()
{
    using T = std::result_of_t<decltype(&foo)(int)>;
    T t;
    return 0;
}

Solution

  • but what does (int) outside of decltype?

    It confuses us. std::result_of is defined in a "cute" way. It's specialized for F(Args...). Where F is the functor type, while Args... are the arguments being fed to it.

    In your case, (int) is the arguments to the call.

    This, and certain other quirks of the definition, is why std::result_of was deprecated in C++17, and replaced with std::invoke_result.