【发布时间】:2019-12-16 08:02:05
【问题描述】:
我有一种动态元组结构:
template <typename... Elems> //Should only be tuples
class DynamicTuple {
vector<byte> data; //All data is stored contiguously
vector<tuple<size_t,size_t>> element_table; //First element is offset into the data vector; second is which index of the parameter pack holds the stored type.
/* ... */
}
现在我希望能够过滤掉所有包含类型列表的元组。
template <typename... Ts>
vector<tuple<Ts&...>> filter() {
vector<tuple<Ts&...>> result;
for (auto it : element_table) {
auto [offset, type] = it;
// ???
}
}
这里我需要能够检查“Elems”参数包的第N个索引中的类型是否是一个包含“Ts”参数包中所有类型的元组。如果是这样,我想推回一个包含这些值的元组。
直观地说,我想使用“type”值从“Elems”参数包中获取类型,并使用类似以下答案的 has_type 结构:https://stackoverflow.com/a/41171291/11463887 类似:
((has_type<Ts, tuple_element<type, tuple<Elems...>>::type>&& ...))
但这不起作用,因为“type”不是编译时常量表达式。 有没有办法解决这个问题?
【问题讨论】:
-
decltype(type)? -
@walnut 这将阻止它变得动态。我明白这就是为什么我的方式,使用 tuple_element 行不通的原因——我要求的是一种在运行时执行此操作的方法。
-
@walnut 我的意思是动态的,就像 std::vector 是动态的,而 std::array 不是。我在运行时将各种类型存储在数据向量中,并且需要 element_table 来查找存储在其中的类型。如果我推回数据向量中的浮点数,我还需要推回 { data.end(0), Index
::value } (取自stackoverflow.com/a/26169248/11463887),所以 element_table 存储DynamicTuple 持有的每个元组都有一个元素。
标签: c++ templates tuples variadic-templates