简体   繁体   English

使用 args 传递函数并将值返回给另一个函数

[英]Pass function with args and return value to another function

Is it possible to pass function with arguments like this:是否可以使用这样的参数传递函数:

int func(int num)
{
 return num;
}

to this function:到这个功能:

        template<class T>
        auto addTask(T task) -> std::future<decltype(task())>
        {
            auto wrapper = std::make_shared<std::packaged_task<decltype(task()) ()>>(std::move(task));
            {
                std::unique_lock<std::mutex> lock(mEventMutex);
                mTasks.emplace([=] {
                    (*wrapper)();
                });
            }
            mEventVar.notify_one();
            return wrapper->get_future();            
        }

I know how pass function without agrs like this:我知道如何在没有 ags 的情况下传递函数,如下所示:

void func()
{}

It will be just:它将只是:

addTask(func);

but can't find where to place args.但找不到放置参数的位置。 Is it possible?是否可以?

You could use a lambda that calls the function:您可以使用调用该函数的lambda

addTask([some_value]()
{
    func(some_value);
});

Or rewrite addTask to use template parameter packs for possible arguments:或者重写addTask以使用模板参数包作为可能的参数:

template<typename Func, typename ...Args>
auto addTask(Func task, Args... arguments) -> std::future<decltype(task(arguments...))>
{
    auto wrapper = std::make_shared<std::packaged_task<decltype(task(arguments...)) (Args...)>>(std::move(task));
    {
        std::unique_lock<std::mutex> lock(mEventMutex);
        mTasks.emplace([=] {
            (*wrapper)(std::forward<Args>(arguments)...);
        });
    }
    mEventVar.notify_one();
    return wrapper->get_future();         
}

addTask(func, some_value);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有args且参数值为c ++的返回值的模板传递成员函数 - Template pass member function with args and return value as parameter c++ 如何将带有args的成员函数作为参数传递给另一个成员函数? - how to pass a member function with args as an argument to another member function? 如何将返回值传递给另一个 function 并将返回值分配给该 function 内的变量? - How do I pass a return value to another function and assign the return value to a variable within that function? 如何将值返回到另一个函数 - How to return a value to another function 将指针传递给数组作为函数参数,哪个本身就是另一个函数的返回值? - Pass pointer to array as function argument, which itself is return value of another function? int fun(void ** args)并在调用函数时传递参数fun(args), - int fun(void **args) and on calling of function pass the argument fun(args), 使用可变参数 Function 将 args 传递给向量 - Using Variadic Function to pass args into a vector 是否可以展开可变参数(lambda)模板并将这些函数的返回值传递给另一个可变参数函数? - Is it possible to unfold a variadic (lambda) template and the pass those functions return value to another variadic function? 如何通过引用另一个需要具有相同参数和相同返回值的函数的库函数来传递对象函数 - How could pass an object function by reference to another library funcion that needs a funcion with the same args and the same returns 一个检测参数类型,值以及另一个函数的名称和返回值的函数? - A function that detects the parameter type, value, and the name and return value of another function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM