简体   繁体   English

在初始化列表中展开可变参数 arguments 失败

[英]Expand variadic arguments in initializer list fails

I try to create a simple program in which I want to create vector of future arguments. I created a wrapper function which is used to submit lambda functions and stores internally in a vector the future objects我尝试创建一个简单的程序,我想在其中创建未来 arguments 的向量。我创建了一个包装器 function,用于提交 lambda 函数并在向量内部存储未来对象

I use an intermediate step in which I create an initiliazer_list using variadic arguments. But fails to compile.我使用了一个中间步骤,在该步骤中我使用可变参数 arguments 创建了一个 initiliazer_list。但无法编译。 I try to use to call a function in order to push the elements in the vector and fails to compile as well我尝试使用调用 function 来推送向量中的元素,但也无法编译

Below is the code下面是代码

#include <iostream>
#include <thread>
#include <future>
#include <functional>
#include <cstdlib>
#include <chrono>
#include <initializer_list>
using namespace std;
using FunctPtr  = function<int(int, int) >;
using FutureInt = future<int>;
using AsyncVector = vector<FutureInt>;
AsyncVector asyncVec;

template<typename... TemplatePtr>
void submit(TemplatePtr... pFunc)
{
  auto initList {pFunc... };
  for (auto & element : initList)
  {
      asyncVec.emplace_back(async(launch::async, element,4,5));
  }
}


int main()
{
    int a;
    int b;
    auto addPtr = [](int x, int y)->int 
    {
        std::cout << "add :" << x + y << std::endl;
        return x + y; 
    };
    auto multPtr = [](int x, int y)->int
    {
        std::cout << "mult :" << x * y << std::endl;
        return x * y;
    };
   // submit(add,4,5);
    submit(addPtr, multPtr);
    for (auto & v : asyncVec)
    {
        std::cout << "Wait for " << v.get() << std::endl;
    }
}

Yes, they are of different types so cannot be in the same init-list easily.是的,它们是不同的类型,所以不能轻易地在同一个初始化列表中。

Your best options should probably be:您最好的选择可能应该是:

  1. Either push them all into asyncVec in the same fold-expression.要么将它们全部推送到同一个折叠表达式中的asyncVec中。
template<typename... TemplatePtr>
void submit(TemplatePtr... pFunc)
{
  (asyncVec.emplace_back(async(launch::async, std::move(pFunc), 4, 5)), ...);
}
  1. Or, if they all are of the same signature, type-erase them, like keeping them in an array of std::function .或者,如果它们都具有相同的签名,则对它们进行类型擦除,例如将它们保存在std::function的数组中。
template<typename... TemplatePtr>
void submit(TemplatePtr... pFunc)
{
  for (auto &&element: {std::function<int(int, int)>(std::move(pFunc))...})
  {
      asyncVec.emplace_back(async(launch::async, std::move(element), 4, 5));
  }
}

(I have specified function signature explicitly though compiler should be able to deduce it.) (我已经明确指定了 function 签名,尽管编译器应该能够推断出它。)

  1. Or, if all closures are captureless and of the same signature, simply cast them to the same type when calling submit :或者,如果所有闭包都是无捕获的并且具有相同的签名,则只需在调用submit时将它们转换为相同的类型:
using SameType = int (*)(int, int);
submit(static_cast<SameType>(addPtr), static_cast<SameType>(mulPtr));

This way your original submit should work as is.这样您的原始submit应该按原样工作。

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

相关问题 传递可变参数时在成员初始值设定项列表中进行转换 - Casting in member initializer list when passing variadic arguments List-initializer和variadic构造函数 - List-initializer and variadic constructor Variadic模板不使用初始化列表 - Variadic template not working with an initializer list 如何通过可变参数模板将多个构造函数 arguments 转发到数组初始值设定项列表? - How to forward multiple constructor arguments through a variadic template to an array initializer list? 如何在初始化列表中使用可变参数 arguments 在编译时包含不同数量的对象? - How to include different number of objects at compile time with variadic arguments in the initializer list? 我们不能从initializer_list创建std :: array,但是可以使用带有可变参数的辅助函数来创建它吗? - We cannot create an std::array from an initializer_list, but can we create it with a helper function with variadic arguments? arguments的转发变量列表 - Forwarding variadic list of arguments C ++将可变参数模板参数扩展为语句 - C++ expand variadic template arguments into a statement 将结构体字段的类型扩展为可变参数模板 arguments - Expand types of struct fields into variadic template arguments 是否可以在可变参数模板函数中扩展非可变参数? - Is it possible to expand non-variadic arguments in a variadic template function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM