【发布时间】:2020-07-15 16:33:19
【问题描述】:
我正在尝试运行以下代码来安排 A 类中的 taskFun 在 1000 毫秒后开始运行。当我运行这段代码时,我得到了这个错误:
main.cpp:9:70: error: no type named 'type' in 'std::__1::result_of<void (A::*(int))(int)>'
std::function<typename std::result_of < callable(arguments...)>::type() > task(std::bind(std::forward<callable>(f), std::forward<arguments>(args)...));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
main.cpp:29:10: note: in instantiation of function template specialization 'later<void (A::*)(int), int>' requested here
later(1000, true, &A::taskFun, 101);
^
如果我将 taskFun 定义为静态函数,我不会收到此错误。但我不希望它是静态的。有没有办法更新“稍后”功能以接受非静态输入?
#include <functional>
#include <chrono>
#include <future>
#include <cstdio>
#include <iostream>
template <class callable, class... arguments>
void later(int after, bool async, callable f, arguments&&... args) {
std::function<typename std::result_of < callable(arguments...)>::type() > task(std::bind(std::forward<callable>(f), std::forward<arguments>(args)...));
if (async) {
std::thread([after, task]() {
std::this_thread::sleep_for(std::chrono::milliseconds(after));
task();
}).detach();
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(after));
task();
}
}
class A {
public:
A() {
}
void callLater() {
later(1000, true, &A::taskFun, 99);
}
void taskFun(int a) {
std::cout << a << "\n";
}
};
【问题讨论】:
标签: c++ visual-c++ c++17