C 17结合了std :: async和std :: Invoke
C++17 combine std::async and std::invoke
我试图从std :: async调用std ::调用,但是由于某种原因,编译器不喜欢它。
注意:我知道我可以使用lambda,但是我想在没有它的情况下使它起作用
这是我的例子。
#include <iostream>
#include <future>
#include <string>
#include <functional>
#include <type_traits>
#include <unistd.h>
template <class Fn, class... Args>
inline std::result_of_t<Fn&&(Args&&...)> runTerminateOnException(Fn&& fn, Args&&... args) {
try {
return std::invoke(std::forward<Fn>(fn), std::forward<Args>(args)...);
} catch (...) {
std::terminate();
}
}
struct A {
static void g(double x, std::string *s) {
std::cout << "g() : x = " << x << ", *s = " << *s << std::endl;
usleep(100);
}
static void f(double x, std::string *s) {
std::invoke(g, x, s); // Working
auto future1 = std::async(std::launch::async, g, x, s); // Working
auto future2 = std::async(std::launch::async, std::invoke, g, x, s); // Not working
auto future3 = std::async(std::launch::async, runTerminateOnException, g, x, s); // Not working
}
};
int main() {
std::string s = "Hello";
A::f(10., &s);
return 0;
}
谢谢您的帮助。
http://coliru.stacked-crooked.com/a/51f8c8c80b0a05fa0e
std::invoke
是模板函数。因此,简单地命名模板名称是模棱两可的 - 您的意思是哪一组无限的std::invoke<F, Args...>
?
您需要提供"调用"混凝土对象。
eg:
#include <iostream>
#include <future>
#include <string>
#include <functional>
#include <type_traits>
#include <unistd.h>
template <class Fn, class... Args>
inline std::result_of_t<Fn&&(Args&&...)> runTerminateOnException(Fn&& fn, Args&&... args) {
try {
return std::invoke(std::forward<Fn>(fn), std::forward<Args>(args)...);
} catch (...) {
std::terminate();
}
}
struct invoker
{
template<class F, class...Args>
decltype(auto) operator()(F&& f, Args&&...args) const {
return std::invoke(f, std::forward<Args>(args)...);
}
};
struct A {
static void g(double x, std::string *s) {
std::cout << "g() : x = " << x << ", *s = " << *s << std::endl;
usleep(100);
}
static void f(double x, std::string *s) {
std::invoke(g, x, s); // Working
auto future1 = std::async(std::launch::async, g, x, s); // Working
auto future2 = std::async(std::launch::async, invoker(), g, x, s); // Working now
// auto future3 = std::async(std::launch::async, runTerminateOnException, g, x, s); // Not working
}
};
int main() {
std::string s = "Hello";
A::f(10., &s);
return 0;
}
与模板函数runterminateOnexception相同。
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 指向std::invoke中成员函数对象的指针
- 将指向数据成员的指针传递给 std::invoke 时有哪些用例和有用性?
- 为什么"std::all_of"不使用"std::invoke"?
- 为什么 std::invoke 不处理函数重载
- 错误 C2893 无法专门化函数模板'unknown-type std::invoke(_Callable &&,_Types &&...)'
- 无法专用化函数模板'unknown-type std::invoke(_Callable &&,_Types &&...) noexcept(<expr>)'
- 无法通过 std::ref() 使用 auto& 参数调用 std::invoke()
- C 17结合了std :: async和std :: Invoke
- C++:创建线程时出现问题;错误 C2672:"std::invoke":找不到匹配的重载函数
- What is std::invoke in c++?
- std::is_invocable 是假的,但 std::invoke 有效
- 错误:“ Invoke”不是“ STD”的成员
- 编译器无法处理std::invoke
- 使用 std::invoke 调用模板化函数
- 何时使用 std::invoke 而不是简单地调用可调用的?
- 错误:'Failed to specialize function template' C2893 'std::invoke'
- 修改后的std::invoke/std::apply,将可调用项作为void*-possible
- std::invoke(C++1z) 如何工作