如果将参数包装到一个元组中,那就非常简单了:
#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;
}
Output:
std::tuple<double, char>
您需要做的就是使用std::tuple<args_t...>
你刚刚在哪里args_t...
眼下。
这是将这个想法构建为更容易处理的另一种想法:
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)
{
// ...
}
};
Usage:
selector<int, char, bool, double>::call<0, 2>(1, true); // int, bool