用另一个元组中的元素填充一个元组
Populating a tuple with elements in another tuple
在下面这样的模板中,如何从另一个更复杂的元组中的元素填充元组?
template<typename... Ts>
struct foo {
std::tuple<std::vector<Ts>...> tuple;
foo() {
//populate tuple somehow
//assume that no vector is empty
}
void func() {
std::tuple<Ts...> back_tuple; // = ...
//want to populate with the last elements ".back()" of each vector
//how?
}
};
我找不到任何元组的push_back机制,所以我不知道如何使用模板循环技巧来做到这一点。此外,我找不到任何类似initializer_list的模板来收集我的值,然后传递到新的元组中。有什么想法吗?
试试这样的东西:
std::tuple<std::vector<Ts>...> t;
template <int...> struct Indices {};
template <bool> struct BoolType {};
template <int ...I>
std::tuple<Ts...> back_tuple_aux(BoolType<true>, Indices<I...>)
{
return std::make_tuple(std::get<I>(t).back()...); // !!
}
template <int ...I>
std::tuple<Ts...> back_tuple_aux(BoolType<false>, Indices<I...>)
{
return back_tuple_aux(BoolType<sizeof...(I) + 1 == sizeof...(Ts)>(),
Indices<I..., sizeof...(I)>());
};
std::tuple<Ts...> back_tuple()
{
return back_tuple_aux(BoolType<0 == sizeof...(Ts)>(), Indices<>());
}
(魔术发生在标记为!!
的行中。)
相关文章:
- 在boost::hana中给定一个键元组,如何从映射中获取值元组
- 从 c++ 中的字符串向量创建一个元组
- 用一个额外的元素扩展 std::array 的每个 std::元组
- 使用对另一个元组不同类型的元素的非常量引用来初始化元组的元素
- 您可以static_assert一个元组只有一种满足特定条件的类型
- 使用参数包和元组创建一个简单的表达式类
- 如何将多个可变参数模板元组类组合成一个类?
- 我如何打印一个元组矢量
- 初始化boost :: hana ::元组用一个参数
- 如何创建一个 std::tuple,其中包含由索引元组指定的向量中的成员
- c++存储映射元组的引用,以便在另一个函数中使用
- 接受元组并返回另一个元组的功能
- 做一个CPP分类的元组
- 将参数一个接一个地传递,或通过将它们包裹在数组,结构或元组中
- 创建一个伪元组,一个存储在其他地方的数据的前端
- 如何在运行时创建一个由N个整数组成的元组
- 在另一个 std::元组中创建 std::"contained" 类型的元组
- 将一个std::数组拆分为一个较小大小的std::array元组
- 填充一个boost元组以替代此方法
- 元组的一个元素可以引用另一个元素吗