我怎样才能通过std::integer_sequence
作为元函数的模板参数(即不是函数模板)?
给定例如以下用例(但不限于此):
我想使用整数序列来删除最后一个N
来自参数包的类型。我以为我可以用selector
from 这个问题 https://stackoverflow.com/a/19759105/678093,但我无法将整数序列传递给这个元函数。
#include <tuple>
#include <utility>
template <typename T, std::size_t... Is>
struct selector
{
using type = std::tuple<typename std::tuple_element<Is, T>::type...>;
};
template <std::size_t N, typename... Ts>
struct remove_last_n
{
using Indices = std::make_index_sequence<sizeof...(Ts)-N>;
using type = typename selector<std::tuple<Ts...>, Indices>::type; // fails
};
int main()
{
using X = remove_last_n<2, int, char, bool, int>::type;
static_assert(std::is_same<X, std::tuple<int, char>>::value, "types do not match");
}
编译器错误
main.cpp:15:55: error: template argument for non-type template parameter must be an expression
using type = typename selector<std::tuple<Ts...>, Indices>::type; // fails
^~~~~~~
main.cpp:5:38: note: template parameter is declared here
template <typename T, std::size_t... Is>
live on coliru http://coliru.stacked-crooked.com/a/d8d1efd56d529309
我将如何传递整数序列?