切片整数参数包

Slice integer parameter pack

本文关键字:参数 整数 切片      更新时间:2023-10-16

我有一个类,它[a,b,...,y,z]获取整数参数包。我需要将其扩展为两个包[a,...,y][b,...,z]- 即删除一次第一个和一次最后一个。最终产品应该是一堆大小为[a,b][b,c]等的二维阵列,直到[x,y][y,z]。我正在尝试使用这样的东西:

std::tuple< std::array< std::array< int, /*RemoveFirst*/Ints>, /*RemoveLast*/Ints>...>

我也对其他解决方案持开放态度。

例:

template <int... Ints>
struct S {
std::tuple< std::array< std::array< int, /*RemoveFirst*/Ints>, /*RemoveLast*/Ints>...> t;
};
int main() {
S<2,3,4> a;
std::get<0>(a.t)[0][0] = 42;
// explenation:
// a.t               is tuple<array<array<int,3>,2>,array<array<int,4>,3>>
// get<0>(a.t)       is array<array<int,3>,2>
// get<0>(a.t)[0]    is array<int,3>
// get<0>(a.t)[0][0] is int
}

我建议以下代码

#include <array>
#include <tuple>
#include <utility>
#include <type_traits>
template <typename T, std::size_t ... Is>
auto constexpr bar (std::index_sequence<Is...> const &)
{ return
std::tuple<
std::array<
std::array<int,
std::tuple_element_t<Is+1u, T>::value>,
std::tuple_element_t<Is, T>::value>...
>{}; }
template <std::size_t ... Is>
auto constexpr foo ()
{ return bar<std::tuple<std::integral_constant<std::size_t, Is>...>>
(std::make_index_sequence<sizeof...(Is)-1u>{}); }
int main ()
{
constexpr auto f = foo<2u, 3u, 5u, 7u, 11u>();
using typeTuple = std::tuple<
std::array<std::array<int, 3u>, 2u>,
std::array<std::array<int, 5u>, 3u>,
std::array<std::array<int, 7u>, 5u>,
std::array<std::array<int, 11u>, 7u>>;
static_assert( std::is_same<typeTuple const, decltype(f)>::value, "!" );
}