【发布时间】:2015-08-08 12:24:15
【问题描述】:
如何将std::integer_sequence 作为模板参数传递给元函数(即不是函数模板)?
给定例如以下用例(但不限于此):
我想使用整数序列从参数包中删除最后一个 N 类型。我以为我可以使用this SO question 中的selector,但我无法将整数序列传递给这个元函数。
#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>
如何传递整数序列?
【问题讨论】: