【发布时间】:2020-10-29 04:09:16
【问题描述】:
我想模拟一个计时器,它会定期调用一个函数(回调),因为我在 sn-p 之后编写了这个函数(虽然很天真),其中,
主函数中的 Timer/
#include <functional>
#include <chrono>
using namespace std::chrono_literals;
template<typename ...Args>
class Timer
{
public:
Timer(std::function<void(Args...)> func, Args&&... args, std::chrono::milliseconds step)
: m_callback{ func },
m_args{ std::forward<Args>(args)... },
m_time{ step },
m_stop{ false },
{ }
private:
std::function<void(Args...)> m_callback;
std::thread m_executer;
std::tuple<Args...> m_args;
std::chrono::milliseconds m_time;
bool m_stop;
};
int main()
{
// Create a timer
auto timer = Timer/*<int, int, float>*/([](int a, int b, float c) { }, 15, 17, 12.0f, 500ms);
return 0;
}
【问题讨论】:
-
我认为它失败了,因为第一个参数是 lambda 而不是 std::function。