基于给定的索引集合获得参数包的子集 [英] Get the subset of a parameter pack based on a given set of indices
问题描述
好的,这是一个非常困难的一个。
Okay, this is a really difficult one.
我想通过在给定的集合中选择参数类型来获得参数包的子集有效索引,然后使用该参数包作为函数的参数列表。 IE:
I want to be able to get the subset of a parameter pack by choosing the parameter types at a given set of valid indices and then use that parameter pack as the argument list of a function. IE:
template <size_t... indices_t>
void func(pack_subset<indices_t..., args_t...>); //incorrect syntax,
//not sure how else to write this
//args_t is predetermined, this
//function is a static member of
//a variadic class template
//For a parameter pack <float, double> and index set 0
void func(float); // <- result of template
我可以得到单个索引的类型,的索引有点难度,特别是当该集合是可变大小时。其语法是:
I can get the type of a single index, but a set of indices is a bit harder, especially when that set is of a variable size. The syntax for that is:
pack_index<size_t index_t, typename... args>::type
也许我可以串起一堆这些在一起,但我不知道如何去扩展indices_t,
Maybe I can string a bunch of these together, but I'm not sure how to go about expanding indices_t so that I get a pack_index for each value in the list.
推荐答案
如果你将参数包装成一个元组,这是非常简单的:
If you wrap the arguments into a tuple, it's pretty straight-forward:
#include <tuple>
template <typename T, std::size_t ...Is>
struct selector
{
using type = std::tuple<typename std::tuple_element<Is, T>::type...>;
};
输入示例:< int,double,float,char,bool& ,1,3
#include <iostream>
#include <demangle.hpp>
int main()
{
std::cout
<< demangle<selector<std::tuple<int, double, float, char, bool>, 1, 3>::type>()
<< std::endl;
}
输出:
std::tuple<double, char>
所有你需要做的是使用 std :: tuple< args_t ..
All you need to do is use std::tuple<args_t...>
where you have just args_t...
at the moment.
这里有一个替代的想法,将这个想法构造成更容易处理的东西:
Here's an alternative idea for structuring that idea into something easier to handle:
template <typename ...Args> struct selector
{
using T = std::tuple<Args...>;
template <std::size_t ...Is>
static void call(typename std::tuple_element<Is, T>::type ...args)
{
// ...
}
};
用法:
selector<int, char, bool, double>::call<0, 2>(1, true); // int, bool
这篇关于基于给定的索引集合获得参数包的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!