我有一种动态元组结构:
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“参数包的Nth索引中的类型是否是包含"Ts”参数包中所有类型的元组。如果是这样的话,我想推回包含这些值的元组。
直观地,我想使用" type“值从"Elems”参数包中获取类型,并使用类似于这个答案的has_type结构:https://stackoverflow.com/a/41171291/11463887,类似于:
((has_type<Ts, tuple_element<type, tuple<Elems...>>::type>&& ...))但是,这是行不通的,因为"type“不是编译时常量表达式。有办法绕过这件事吗?
发布于 2019-12-16 09:17:27
使用您已经拥有的has_type,您可以以相同的方式定义一个type_subset,以测试是否所有类型都包含在元组中:
template <typename Ts, typename Tuple>
struct type_subset;
template <typename... Ts, typename... Us>
struct type_subset<std::tuple<Ts...>, std::tuple<Us...>>
: std::conjunction<has_type<Ts, std::tuple<Us...>>...> {};然后,您可能希望通过迭代参数包和相应的索引来找到与type索引匹配的正确类型:
size_t i = 0;
bool match = ((type == i++ && type_subset<std::tuple<Ts...>, Elems>::value) || ...);确保在每次执行折叠表达式之前将i重置为0。您可能希望将整个过程放入lambda或函数中,以分离i并与其他条件重用。
可能更好的执行方式是在编译时将可能的结果保存到数组中,然后在运行时将索引保存到数组中:
constexpr static std::array matches{type_subset<std::tuple<Ts...>, Elems>::value...};
bool match = matches[type];在这两种情况下,您都需要确保type < sizeof...(Elems)。特别是在第二个变体中,否则您将有未定义的行为(如果您不使用.at而不是[])。
发布于 2019-12-16 08:35:13
但是,不起作用,因为"type“不是编译时常量表达式。有办法绕过这件事吗?
不可能基于运行时值返回不同的类型。
您要么不得不求助于可以在运行时进行评估的方法(例如。使用vector)或具有输入被编译时间常数。
运行时解决方案可以使用type_id或模拟。
https://stackoverflow.com/questions/59352540
复制相似问题