0

当 Callable 返回模板类型并出现以下错误(clang++)时,我正在尝试使用 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;
    }
4

1 回答 1

1

g<int>shared_ptr<int>()由函数推导时衰减为指向该类型 ( shared_ptr<int>(*)()) 的指针的类型。因此FunctionTypein不是模板,您不能在其上使用模板参数。submit

如果您可以更清楚地了解您要做什么,我们可以为您的主要问题找到解决方案。

于 2016-11-05T02:50:47.443 回答