C++ overloaded function pointer
I am not able to get this to work:
template<class Input, class Output, class Inde开发者_高级运维x>
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);
compiler message I get tells me that copy is not resolved, instead I get
unresolved overloaded function type>
.
what am I doing wrong? thank you
well, I missed return type of std::copy which is output iterator type.
Correct code :
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);
You could make a design change. One might be make the return type a separate template parameter:
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);
The return type is deduced (and subsequently ignored by your code.) The other option, which I would recommend, would be to accept any generic function type:
template<class CopyFunc, class Input, class Output, class Index>
size_t pack(CopyFunc func,
size_t N, Input input, Output output,
const Index &index);
This doesn't enforce any specific signature, and provides the most flexibility.
精彩评论