2010-07-27 129 views
1

我無法得到這個工作:C++重載函數指針

template<class Input, class Output, class Index> 
size_t pack(void (*copy)(Input, Input, Output), 
      size_t N, Input input, Output output, 
      const Index &index); 

size_t K = pack(&std::copy<const double*,double*>, 
       M, C.data().begin(), C_.data().begin(), 
       index.host); 

編譯器的消息,我得到告訴我該副本沒有解決,而不是我得到 unresolved overloaded function type>

我做錯了什麼? 謝謝

+1

壞人,我發現問題,std :: copy返回輸出不是void。 我應該刪除這個問題嗎? – Anycorn 2010-07-27 03:27:22

+0

你可以寫,然後接受你自己的答案。好於deletin'。 – 2010-07-27 03:37:46

回答

1

你可以做一個設計變更。一個可能使返回類型的單獨的模板參數:(以及隨後由代碼忽略)

template<class R, class Input, class Output, class Index> 
size_t pack(R (*copy)(Input, Input, Output), 
      size_t N, Input input, Output output, 
      const Index &index); 

返回類型推導的另一種選擇,我會建議,將接受任何泛型函數類型:

template<class CopyFunc, class Input, class Output, class Index> 
size_t pack(CopyFunc func, 
      size_t N, Input input, Output output, 
      const Index &index); 

這不強制任何特定的簽名,並提供最大的靈活性。

+0

謝謝。第一個是我需要的。 – Anycorn 2010-07-27 04:14:16

2

好吧,我錯過了輸出迭代器類型的std :: copy的返回類型。

正確的代碼:

template<class Input, class Output, class Index> 
size_t pack(Output (*copy)(Input, Input, Output), 
      size_t N, Input input, Output output, 
      const Index &index);