[英]How to parse parameter pack as template parameter
大家。最近想寫一個函數來統計最常見情況下的時間消耗。
template<typename ReturnType, typename... ArgList>
int64_t TimeCount(function<ReturnType(ArgList...)> f, ArgList... args) {
auto t0 = chrono::high_resolution_clock::now();
f(std::forward<ArgList>(args)...);
auto t1 = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(t1 - t0).count();
cout << "cost " << duration << "ms " << endl;
return duration;
}
但是使用上面的代碼,只candidate template ignored: could not match 'function<type-parameter-0-0 (type-parameter-0-1...)>' against 'void (*)(int (*)(std::vector<long> &, int, int), std::vector<long> &, int, int)'
我能得到,有什么方法可以使代碼正常工作嗎?
謝謝閱讀。
問題
template <typename ReturnType, typename... ArgList>
int64_t TimeCount(std::function<ReturnType(ArgList...)> f, ArgList... args)
是
ArgList
應該從 2 個地方推導出來並且應該是相同的。std::function
,並且不允許推論。您可以改用:
template <typename F, typename... ArgList>
int64_t TimeCount(F f, ArgList&&... args)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.