包装到std :: async()无效 [英] Wrapper to std::async() not working

查看:85
本文介绍了包装到std :: async()无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:对std :: bind()的调用可以替换为其他东西,我只希望runAsyncTerminateOnException()与std :: async()具有相同的签名,就像对其进行包装一样



我正在尝试为std :: async()创建一个包装器。
您知道直接调用std :: async()时如何使包装器正常工作吗?



注意:我不会修改print()函数签名,这是一个示例。我希望包装器是通用的,并且可以通过直接调用std :: async()来处理好所有可能的参数。



谢谢。 / p>

http://ideone.com/HbBqeo

  #include< iostream> 
#include< functional>
#include< future>

template< class Fn,class ... Args>
内联自动runAsyncTerminateOnException(Fn& fn,Args& amp; ... args){
auto make_call = std :: bind(std :: forward< Fn>(fn),std :: forward< Args>(args)...);

返回std :: async(std :: launch :: async,[=]()-> decltype(make_call()){
try {
return make_call( );
} catch(...){
std :: cout<<终止调用!<< std :: endl;
std :: terminate() ;
}
});
}

struct Foo {
template< class ... Args>
void print(Args& amp; ... args){
printf( Foo :: print(%d)\n,std :: forward< Args>(args)... );
}
};

int main(){
Foo foo;
std :: future< void> future = std :: async(std :: launch :: async,& Foo :: print< int> ;,& foo,2);
std :: future< void> future2 = runAsyncTerminateOnException(& Foo :: print< int> ;,& foo,2);
//您的代码在这里
返回0;
}


解决方案

我找到了c的解决方案++ 17。
仅当我们不对runTerminateOnException()的返回类型使用auto时才起作用。

  template< class Fn ,课程... Args> 
内联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();
}
}

template< class Fn,class ... Args>
内联自动runAsyncTerminateOnException(Fn& fn,Args& amp; ... args){
返回std :: async(std :: launch :: async,runTerminateOnException< Fn,Args&& ...> ;, std :: forward Fn(fn),std :: forward Args(args)...);
}


EDIT : The call to std::bind() can be replaced with something else, I just want runAsyncTerminateOnException() to work with the same signature than std::async(), like just a wrapper to it

I am trying to create a wrapper to std::async(). Do you know how to make the wrapper working as well when a direct call to std::async() works ?

Note : I will not modify the print() function signature, this is an example. I would like the wrapper to be generic and to work for every possible parameters that are well handled by a direct call to std::async().

Thank you.

http://ideone.com/HbBqeo

#include <iostream>
#include <functional>
#include <future>

template<class Fn, class... Args>
inline auto runAsyncTerminateOnException(Fn&& fn, Args&&... args) {
    auto make_call = std::bind(std::forward<Fn>(fn), std::forward<Args>(args)...);

    return std::async(std::launch::async, [=]() -> decltype(make_call()) {
        try {
            return make_call();
        } catch (...) {
            std::cout << "Terminate Called!" << std::endl;
            std::terminate();
        }
    });
}

struct Foo {
    template<class... Args>
    void print(Args&&... args) {
        printf("Foo::print(%d)\n", std::forward<Args>(args)...);
    }
};

int main() {
    Foo foo;
    std::future<void> future = std::async(std::launch::async, &Foo::print<int>, &foo, 2);
    std::future<void> future2 = runAsyncTerminateOnException(&Foo::print<int>, &foo, 2);
    // your code goes here
    return 0;
}

解决方案

I found the solution for c++17. It works only if we do not use auto for the return type of runTerminateOnException().

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();
    }
}

template<class Fn, class... Args>
inline auto runAsyncTerminateOnException(Fn&& fn, Args&&... args) {
    return std::async(std::launch::async, runTerminateOnException<Fn, Args&&...>, std::forward<Fn>(fn), std::forward<Args>(args)...);
}

这篇关于包装到std :: async()无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆