c++ - 将相同的参数转发给 C++ 中的可变元组构造函数
问题描述
TL; DR 下面
我正在尝试使用用于多核处理的包编写一些 C++ 代码。该包有一个很好的发送者类,我用它在线程之间发送消息。它看起来像这样:
// structs provided by the package
struct GlobalVarsContainer {
// some global vars
};
template<typename ...Ts>
struct sender {
GlobalVarsContainer* my_container;
sender (GlobalVarsContainer& c) : my_container(&c);
void send(Ts... args) {
// send the messages of types Ts
};
};
这个类的构造函数实际上涉及更多,所以我希望构造函数只调用一次。我坚持为我需要的所有发件人使用容器类型,所以是这样的:
typedef std::tuple<
sender<uint32_t,uint32_t>,
sender<uint32_t,double>,
sender<uint32_t,double>
> sender_container_1_t;
typedef std::tuple<
sender<uint32_t,double>,
sender<uint32_t,double>,
sender<uint32_t,double>
> sender_container_2_t;
现在我可以构造 asender_container_1_t
并通过引用我的所有函数来传递它,万岁!但这很难看,您必须像这样实例化容器:
GlobalVarsContainer gvc1, gvc2;
sender_container_1_t c1(gvc1, gvc1, gvc1);
sender_container_2_t c2(gvc2, gvc2, gvc2);
我想要一些形式的东西
GlobalVarsContainer gvc1, gvc2;
// Should have a tuple of senders of type sender<uint32_t,uint32_t>, sender<uint32_t,double> and sender<uint32_t,double>, all constructed with a reference to gvc as an argument.
sender_container<uint32_t,double,double> c1(gvc1);
// Should have a tuple of senders of type sender<uint32_t,double>, sender<uint32_t, double> and sender<uint32_t, double>, all constructed with a reference to gvc as an argument.
sender_container<double,double,double> c2(gvc2);
所以我想到了使用可变参数容器结构,如下所示:
// My sender container
template<typename... Ts>
struct SenderContainer {
std::tuple<sender<uint32_t, Ts>...> my_senders;
// What to do for the constructor ?
};
但是我不知道该怎么做才能SenderContainer<uint32_t,double,double> my_container(gvc1)
将其gvc1
转发给所有发件人的构造函数。有人有小费吗?
TL;DR:如果我有一个可变参数结构模板
template<typename ...Ts>
struct Bar {
Bar(Foo f){};
}
我可以制作表格的容器吗
template<typename ...Ts>
struct BarContainer{
std::tuple<Bar<int,Ts>...> bars
}
这样我就可以调用BarContainer<int, double, long> newBarContainer(f)
并将Foo f
转发给BarContainer::bars
.
解决方案
推荐阅读
- c# - 绑定失去它的绑定
- python - 为什么在我调整散景中的滑块后,包含度量名称的 TableColumn 会消失?
- android - 我的问题是如何将 ID 从一项活动传递到另一项活动
- imagemagick - 将 imagettftext GD 修改为 Imagick
- node.js - 为什么 fs.readFile (Node.js) 的文件名参数前有一个点?
- coypu - Coypu 使用 InternetExplorerOptions
- sql-server - 根据接收到的参数值删除过滤器
- c# - Asp.Net Core 防止控制台应用程序关闭
- ios - 我需要帮助使用 Swift 在视图控制器中创建两个单选按钮
- drupal-7 - 为什么 fpdi 输出在 Drupal 7 中不工作但在 D7 之外工作