Search code examples
functionc++11returnresult-of

C++11 result_of deducing my function type failed


I was trying a program below:

#include<type_traits>
using namespace std;
template <class F, class R = typename result_of<F()>::type>
R call(F& f) { return f(); }
int answer() { return 42; }

int main()
{
    call(answer); 
    return 0;
}

"call(answer)" fails to compile

VC says 'R call(F&)' could not deduce template argument for 'R'

GCC says |note: template argument deduction/substitution failed:|error: function returning a function

I'm not sure if a "function name" could be used for templates. Where did I get wrong, how to make my call(answer) work?


Solution

  • You can use forwarding references in these cases:

    #include<type_traits>
    #include<utility>
    #include<cassert>
    
    using namespace std;
    
    template <class F, class R = typename result_of<F()>::type>
    R call(F&& f) { return std::forward<F>(f)(); }
    
    int answer() { return 42; }
    
    int main()
    {
        assert(call(answer) == 42);
        return 0;
    }
    

    It usually avoids troubles.

    That said, why your code doesn't work is nicely explained by @T.C. in his answer.
    See also the comments to this question for further details.