如何將std::integer_sequence
作爲模板參數傳遞給元函數(即不是函數模板)?將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>
我將如何傳遞的整數序列?
難以置信的快! –