2016-11-05 79 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

謝謝!我想我現在明白了。 –

相關問題