2016-11-05 77 views
0

我想使用result_of的情况下,当Callable返回模板类型并获得以下错误(铿锵声++)。我还包括一个简单的例子,一切正常。当返回值是模板类型时如何使用std :: result_of?

错误:

main.cpp:22:50: note: candidate template ignored: could not match '<type-parameter-0-1>' against 'std::__1::shared_ptr<int> (*)()' 
typename std::result_of<FunctionType<T>()>::type submit(FunctionType<T> f) { 

代码:

int f() { 
     int x = 1; 
     return x; 
    } 

    template<typename T> 
    std::shared_ptr<T> g() { 
     std::shared_ptr<T> x; 
     return x; 
    } 

    template <template<typename> class FunctionType, typename T> 
    typename std::result_of<FunctionType<T>()>::type submit(FunctionType<T> f) { 

     using result_type = typename std::result_of<FunctionType<T>()>::type; 

     result_type x; 
     return x; 
    } 

     template<typename FunctionType> 
     typename std::result_of<FunctionType()>::type submit2(FunctionType f) { 

     using result_type = typename std::result_of<FunctionType()>::type; 

     result_type x; 
     return x; 
    } 


    int main() 
    { 
     submit(g<int>); // error 
     submit2(f);  // ok 

     return 0; 
    } 

回答

1

g<int>shared_ptr<int>()类型当由函数推导出衰减到一个指针类型(shared_ptr<int>(*)())的。 FunctionType in submit因此是而不是模板,您不能在其上使用模板参数。

如果你可以更清楚你想要做什么,我们可以找出解决你的主要问题。

+0

谢谢!我想我现在明白了。 –

相关问题