2017-08-16 9 views
1

動作しない、次のように私は、テンプレートテンプレートパラメータを使用:C++ 11の転送テンプレートテンプレートパラメータが

/* [1]: Definition containing a template template parameter */ 
      template <typename T, template<class> class Kernel> 
      void ForEach(Kernel<T> kernel, T * pSrc, int elementCount) { 
       //.... 
      } 

    /* [2]: Definition of a helper struct */ 
      template <typename T> struct KernelStd { 
       //... 
      }; 

    /* [3]: Use the previous definitions */ 
      float arr1[5] = {1,2,3,4,5}; 

      //The following two calls to ForEach do successfully compile 
      ForEach(KernelStd<float>(), arr1, 5); //USE1 
      ForEach<float>(KernelStd<float>(), arr1, 5); //USE2 

    /* [4]: Definition of a helper function */  
      template <typename F, typename ...Args> 
      void forwarder(F func1, Args && ...args) { 
       //... 
       func1(std::forward<Args>(args)...); 
      } 
      //But the following callS do not compile. 
      forwarder(ForEach, KernelStd<float>(), arr1, 5); //USE3 
      forwarder(ForEach<float>, KernelStd<float>(), arr1, 5); //USE4 

私はVS2013更新5を使用していますが、私は次のエラーを取得する:

  error C2783: 'void ForEach(Kernel<T>,T *,int)' : could not deduce 
     template argument for 'Kernel' 

どれでも助けていただければ幸いです。

+0

「ForEach」とはそれは何らかの形で「変換する」ことに関連していますか? 'KernelStd'とは何ですか? 'Kernel1'と何らかの関係がありますか?あなたが[MCVE](https://stackoverflow.com/help/mcve)を表示するのが最善でしょう –

+0

私のコード断片の間違いを指摘してくれてありがとう。私はそれらを修正しました。 – fonishormon

答えて

0

forwarderは関数なので、最初の引数はFの実体(オブジェクトまたは関数)でなければなりません。

ForEachは、機能でもオブジェクトでもなく、テンプレートです。ForEachforwarderに渡すことはできません。

ForEach<float>は完全にForEachテンプレートからインスタンス化機能を識別するのに十分なテンプレート引数を渡すことはありません。テンプレート引数の控除のために、関数テンプレートを呼び出すときにそれを手放すことができます。しかし、forwarderという文脈では、タイプFは最初の引数から推測されるはずなので、ちょっとした鶏と卵の問題があります。

forwarderを使用する場合は、テンプレートではなく実際の関数を指定する必要があります。だから、これを実行する必要があると思います:

forwarder(ForEach<float, KernelStd>, KernelStd<float>(), arr1, 5); 

ForEach<float, KernelStd>は、すべてのテンプレート引数が含まれており、それゆえ(テンプレートからインスタンス化)機能を指定します。

関連する問題