我正在尝试实现一个通用函数,它记录了另一个函数运行的时间。
#include <iostream>
#include <future>
#include <thread>
#include <vector>
#include <functional>
#include <numeric>
#include <memory>
template<typename Res, typename Func, typename...Args>
std::pair<Res, double> ftime(Func fun, Args&&... args)
{
auto start = std::chrono::system_clock::now();
Res res = fun(std::forward<Args>(args)...);
std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
return std::make_pair(res, duration.count());
}
int main ()
{
std::vector<int> values (100, 1);
auto res = ftime(std::accumulate, values.begin(), values.end(), 0);
std::cout << "Sum up " << values.size() << std::endl;
std::cout << "Serial sum = " << res.first << " took : " << res.second << std::endl;
}
上述代码无法编译,并出现以下错误:
sum_1000000.cpp: In function ‘int main()’:
sum_1000000.cpp:22:68: error: no matching function for call to ‘ftime(<unresolved overloaded function type>, std::vector<int>::iterator, std::vector<int>::iterator, int)’
auto res = ftime(std::accumulate, values.begin(), values.end(), 0);
^
sum_1000000.cpp:10:24: note: candidate: template<class Res, class Func, class ... Args> std::pair<Res, double> ftime(Func, Args&& ...)
std::pair<Res, double> ftime(Func fun, Args&&... args)
^
sum_1000000.cpp:10:24: note: template argument deduction/substitution failed:
sum_1000000.cpp:22:68: note: couldn't deduce template parameter ‘Res’
auto res = ftime(std::accumulate, values.begin(), values.end(), 0);
据我所知,编译器无法识别std :: acumulate函数的模板类型。我做错了什么?
谢谢
答案 0 :(得分:2)
代码中存在一些问题。
首先推断函数返回类型:
template<typename Func, typename...Args>
auto ftime(Func fun, Args&&... args) -> std::pair<decltype(fun(std::forward<Args>(args)...)), double>
{
auto start = std::chrono::system_clock::now();
auto res = fun(std::forward<Args>(args)...);
std::chrono::duration<double> duration = std::chrono::system_clock::now() - start;
return std::make_pair(res, duration.count());
}
std::accumulate
不是函数,而是模板。最简单的方法是将调用包装成lambda:
auto res = ftime([&values]() { return std::accumulate(values.begin(), values.end(), 0); });
答案 1 :(得分:1)
编译器无法推断出Res
您的模板,也无法推断出accumulate
的模板参数。
#include <utility>
#include <chrono>
#include <vector>
#include <numeric>
#include <iostream>
template<typename Func, typename...Args>
auto invoke_timed(Func&& fun, Args&&... args) ->
std::pair<decltype(std::forward<Func>(fun)(std::forward<Args>(args)...)),
std::chrono::system_clock::duration::rep>
{
auto start = std::chrono::system_clock::now();
auto res = std::forward<Func>(fun)(std::forward<Args>(args)...);
return std::make_pair(res, (std::chrono::system_clock::now() - start).count());
}
int main()
{
std::vector<int> values(100, 1);
auto res = invoke_timed(std::accumulate<std::vector<int>::iterator, int>,
values.begin(), values.end(), 0);
std::cout << res.first << ", " << res.second << "\n";
return 0;
}